[英]Sending email from a web api
我想知道如何設置我的info@*****.com帳戶以及如何在c#Web API應用程序中發送電子郵件。
我配置了一些這樣的代碼:
SmtpClient client = new SmtpClient();
client.Host = "173.***.**.**";
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = true;
client.EnableSsl = true;
我做錯什么了嗎?
enableSSL可能會引起一些問題,請嘗試將其關閉,然后再從那里進行操作。
看來您至少缺少以下內容:
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("info@*****.com");
mailMessage.To.Add("info@*****.com");
mailMessage.Body = "body";
mailMessage.Subject = "subject";
client.Send(mailMessage);
完整的代碼應該是
SmtpClient SmtpServer = new SmtpClient("smtp.live.com");
var mail = new MailMessage();
mail.From = new MailAddress("yourmail@hotmail.com");
mail.To.Add("tomail@foo.com.br");
mail.Subject = "Test Mail - 1";
mail.IsBodyHtml = true;
string htmlBody;
htmlBody = "Write some HTML code here";
mail.Body = htmlBody;
SmtpServer.Port = 587;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new System.Net.NetworkCredential("yourmail@hotmail.com", "pwd");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.