C# 发送邮件

方法一:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
public static string SendMail2(string add_smtp, string acc_SendMail, string psw_SendMail, string add_SendMail, ArrayList arr_GetMail, ArrayList arr_CcMail, ArrayList arr_McMail, ArrayList arr_Att, string body, string subject, bool bodytype)
       {
           add_smtp = "实际地址";
           psw_SendMail = "密码";
           string result = "";
           System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(add_smtp, 25);
           smtp.Credentials = new System.Net.NetworkCredential(acc_SendMail, psw_SendMail);
           System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
           for (int s = 0; s < arr_GetMail.Count; s++)
           {
               mail.To.Add(arr_GetMail[s].ToString() + "");
           }
 
           //抄送
           for (int c = 0; c < arr_CcMail.Count; c++)
           {
 
               mail.CC.Add(arr_CcMail[c].ToString() + "");
           }
 
 
           //密送
           for (int c = 0; c < arr_McMail.Count; c++)
           {
 
               mail.Bcc.Add(arr_McMail[c].ToString() + "");
           }
 
           mail.SubjectEncoding = System.Text.Encoding.GetEncoding("GB2312");
 
           mail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
 
           mail.From = new System.Net.Mail.MailAddress(add_SendMail);
 
           mail.Priority = System.Net.Mail.MailPriority.Normal;
 
           mail.Subject = subject;
 
           mail.Body = body;
 
 
           mail.IsBodyHtml = bodytype;
 
           for (int d = 0; d < arr_Att.Count; d++)
           {
               System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(arr_Att[d].ToString());
 
               mail.Attachments.Add(attachment);
           }
 
           try
           {
               smtp.Send(mail);
 
               result = "sucess";
           }
           catch (System.Net.Mail.SmtpException ex)
           {
               result = ex.Message + ex.Source;
           }
 
           finally
           {
               mail.Dispose();
           }
           return result;
 
       }

  

方法二:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/// <summary>
    /// 邮件发送辅助类
    /// </summary>
    public class MailHelper
    {
        /// <summary>
        /// 发送邮件 [如果发送失败,不添加重发]
        /// </summary>
        /// <param name="tomail">收件人</param>
        /// <param name="subject">主题</param>
        /// <param name="body">消息内容</param>
        public static void SendMail(string  tomail, string subject, string body,string mailFrom)
        {
            // 设置发送方的邮件信息,例如使用网易的smtp
            string smtpServer = "实际地址"; //SMTP服务器
            string user = "用户名";
            string userPassword = "密码";//登陆密码
            string mailTo = tomail;
            // 邮件服务设置
            SmtpClient smtpClient = new SmtpClient();
            smtpClient.Host = smtpServer; //指定SMTP服务器
            smtpClient.Port = 25;
            smtpClient.UseDefaultCredentials = false;
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
            smtpClient.Credentials = new System.Net.NetworkCredential(mailFrom, userPassword);//用户名和密码
 
 
            // 发送邮件设置       
            MailMessage mailMessage = new MailMessage(mailFrom, mailTo); // 发送人和收件人
            mailMessage.Subject = subject;//主题
            mailMessage.Body = body;//内容
            mailMessage.BodyEncoding = Encoding.UTF8;//正文编码
            mailMessage.IsBodyHtml = true;//设置为HTML格式
            
            try
            {
                smtpClient.Send(mailMessage); // 发送邮件
            }
            catch (SmtpException ex)
            {
                throw ex;
            }
        }
    }

  

 

posted @   小严不言慢  阅读(49)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!
点击右上角即可分享
微信分享提示