简体   繁体   中英

c# send email through work mail

I tried to write c# code to send emails through my work mail account. I already connected the email account to my android smartphone and it's working well. I took the connection data from it and now I'm trying to send email with c# but I can't manage it. I used the same c# code to send email from my gmail account and it work fine, but when I try to use it with my work account I can't. Am I doing something wrong?

My code is:

MailMessage mail = new MailMessage();
mail.To.Add(new MailAddress("myPersonalEmailAddress"));
mail.From = new MailAddress("myWorkEmailAddress");
mail.Subject = "Test subject";
mail.Body = "Test body";

/*
 * The server name, port and domain are similar to smartphone account
 * It also uses SSL so I enabled it
 */
SmtpClient client = new SmtpClient("serverName", 443);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("username", "password", "domain");
client.EnableSsl = true;
client.Timeout = 20000;

try
{
     client.Send(mail);
}

catch (Exception e)
{

}

您应该在您的gmail帐户中允许此操作的访问,然后它将可以正常工作

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM