Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I m using smtp in .net for mail sending. it is working properly at my home.bt in office there is proxy server for domain so what i have to change in code.
The code below is not working in my office.I have already change "smtp.mycompany.com" and pot to 8080 but it is not working.
C#
string SMTPSERVER = "smtp.gmail.com"
int PORTNO =25;
SmtpClient smtpClient = new SmtpClient(SMTPSERVER, PORTNO);
smtpClient.EnableSsl = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
//smtpClient.Timeout = 1500000000;
smtpClient.Timeout = int.MaxValue;
smtpClient.Credentials = new NetworkCredential(gmailUserName, gmailUserPassword);
using (MailMessage message = new MailMessage())
{
    message.From = new MailAddress(gmailUserName);
    message.Subject = subject == null ? "" : subject;
    message.Body = body == null ? "" : body;
    message.IsBodyHtml = isBodyHtml;

    foreach (string email in emailToAddress)
    {
        message.To.Add(email);
    }
    if (ccemailTo != null && ccemailTo.Count > 0)
    {
        foreach (string emailCc in ccemailTo)
        {
            message.CC.Add(emailCc);
        }
    }

    foreach (string str in LstAttachment)
    {
        message.Attachments.Add(new Attachment(str));
    }

    try
    {
        smtpClient.Send(message);
        return "Email Send SuccessFully";
    }
    catch (TimeoutException ex)
    {
        Thread.Sleep(5000);
        try
        {
            smtpClient.Send(message);
            return "Email Send SuccessFully";
        }
        catch
        {
            return "Email Send failed";
        }
    }
    catch (Exception e)
    {
        return "Email Send failed";
    }
    
}


If anybody have solution then plz help me.

Thanks.


[Edit member="Tadit"]
Corrected formatting and/or grammatical issues.
Added pre tags.
[/Edit]
Posted
v2
Comments
What is the exception message?
nidhi86 13-Oct-14 13:19pm    
some times timeout exception and some times simple smtp exception.
Nandakishore G N 13-Oct-14 10:37am    
It is not the problem of your code. You contact your admin to allow/give access for you to send mail or host your application in server and check, it'll work.
nidhi86 13-Oct-14 13:17pm    
thanks for Your reply.
and one more thing I want to ask when I attach a file size of 4 or 5 mb this code sends attached file successfully but time out exception occurred and when attach file size of 1 mb then it works properly without any exception. What changes I have to do?
First of all, make sure that your Email provider allows large files as attachment or not. You can test with Gmail first. Gmail allows upto 25 MB.

One question... How are you reading the file? Are you loading the file to MemoryStream?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900