This article is a mirror article of machine translation, please click here to jump to the original article.

View: 27145|Reply: 2

[Source] C# implements the function of sending emails

[Copy link]
Posted on 1/4/2016 12:56:18 PM | | | |


This is my own test rendering, an email sent to QQ mailbox with my own corporate email,

Here's how:

  1. /// <summary>
  2.         /// 发送邮件
  3.         /// </summary>
  4.         /// <param name="from">发件人邮箱</param>
  5.         /// <param name="fromname">发件人姓名</param>
  6.         /// <param name="to">收件人地址</param>
  7.         /// <param name="subject">标题</param>
  8.         /// <param name="body">内容</param>
  9.         /// <param name="username">发件人账号</param>
  10.         /// <param name="password">发件人密码</param>
  11.         /// <param name="server">发件服务器</param>
  12.         /// <param name="port">端口</param>
  13.         /// <returns></returns>
  14.         public static bool SendMail(string from, string fromname, string to, string subject, string body, string username, string password, string server,int port=25)
  15.         {

  16.             try
  17.             {
  18.                 //邮件发送类
  19.                 MailMessage mail = new MailMessage();
  20.                 //是谁发送的邮件
  21.                 mail.From = new MailAddress(from, fromname);
  22.                 //发送给谁
  23.                 mail.To.Add(to);
  24.                 //标题
  25.                 mail.Subject = subject;
  26.                 //内容编码
  27.                 mail.BodyEncoding = Encoding.Default;
  28.                 //发送优先级
  29.                 mail.Priority = MailPriority.High;
  30.                 //邮件内容
  31.                 mail.Body = body;
  32.                 //是否HTML形式发送
  33.                 mail.IsBodyHtml = true;
  34.                 //邮件服务器和端口
  35.                 SmtpClient smtp = new SmtpClient(server, port);
  36.                 smtp.UseDefaultCredentials = true;
  37.                 //指定发送方式
  38.                 smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
  39.                 //指定登录名和密码
  40.                 smtp.Credentials = new System.Net.NetworkCredential(username, password);
  41.                 //超时时间
  42.                 smtp.Timeout = 10000;
  43.                 smtp.Send(mail);
  44.                 return true;
  45.             }

  46.             catch (Exception exp)
  47.             {
  48.                 //return exp.Message;
  49.                 return false;
  50.             }
  51.         }
Copy code






Previous:get request JsonResult error. There was an error processing your request.
Next:Conversion between jQuery and DOM objects
Posted on 9/10/2019 10:33:05 PM |
Waving his weapons and retreating from the Japanese transportation group

Score

Number of participants1MB-1 contribute-1 Collapse reason
QWERTYU -1 -1 Do not pour water, improving the quality of reply posts is the duty of every member.

See all ratings

Posted on 4/20/2023 9:26:01 AM |
Thanks for sharing
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com