Click here to Skip to main content
15,886,780 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I write code getting exception please resolve it

What I have tried:

public void SendMailNew(string mailTo, string Body, string Subject, string filepath)
    {
        try
        {
            MailMessage msg = new MailMessage();
            msg.IsBodyHtml = false;
            msg.Subject = Subject;
            msg.Body = Body;
            msg.From = new MailAddress("xxxxxxxxxxxxxxxxx");
            string[] multiId = mailTo.Split(';');
            foreach (string mailid in multiId)
            {
                if (mailid.Length != 0)
                {
                    msg.To.Add(new MailAddress(mailid));
                }
            }
            msg.Attachments.Add(new Attachment(filepath));
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "Smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            string Password = ConfigurationManager.AppSettings["SMSPASSWORD"];
            NetworkCredential nc = new NetworkCredential("xxxxxxxxxxxxx", Password);
            smtp.Credentials = nc;
            smtp.Send(msg);
           
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
Posted
Updated 20-Dec-19 2:02am
v4
Comments
Richard Deeming 5-Dec-19 9:13am    
catch (Exception ex)
{
    throw ex;
}

Don't do that. You've just destroyed the stack-trace of the exception, making it much harder to track down the real cause of the exception.

If you have to re-throw an exception, use throw; instead of throw ex;.

But in this case, since you're not doing anything with the exception, just remove the try..catch block.

1 solution

Most probably, google mail server cannot authenticate an account which resides on a domain it does not own.
You should talk to the network admin of your university to know which smtp server to contact to send emails to the outside.
 
Share this answer
 

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