C#实现发送邮件的方法

时间:2022-06-01 21:58:30

本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下

?
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#region 发送邮件部分
    private static String fromMail = "1111@126.com"///邮箱名称
    private static String mailPwd = "111111";     ///密码
    private static string toMail = "2222@163.com"///邮箱服务器
    private static string fileStr;//当前图片路径,在添加附件时用
    /// <summary>
    /// 发送邮件
    /// </summary>
    /// <param name="fileUrl">附件地址,以~分</param>
    /// <param name="screen">是否发送截屏</param>
    /// <returns></returns>
    public static string SendMail(string fileUrl, string screen)
    {
     
      MailAddress from = new MailAddress(fromMail);
      MailAddress to = new MailAddress(toMail);
 
      MailMessage message = new MailMessage(from, to);
      message.Subject = "M邮件 " +11111;//主题
      message.SubjectEncoding = System.Text.Encoding.UTF8;
 
      Attachment attachFile = new Attachment(fileStr);
      if (screen == "True")
        message.Attachments.Add(attachFile);
 
      string[] files = fileUrl.Split('~');
      for (int i = 0; i < files.Length; i++)
      {
        if (File.Exists(files[i]))
        {
          Attachment attachFile1 = new Attachment(fileUrl);
          message.Attachments.Add(attachFile1);
        }
      }
 
      try
      {
        SmtpClient client = new SmtpClient("smtp." + from.Host);
 
        SendMail(client, from, mailPwd, to, message);
 
       
        return "发送邮件成功!包含附件:" + fileUrl + " 含截图?" + screen + "  " + DateTime.Now.ToString();
      }
      catch (SmtpException ex)
      {
        //如果错误原因是没有找到服务器,则尝试不加smtp.前缀的服务器
        if (ex.StatusCode == SmtpStatusCode.GeneralFailure)
        {
          try
          {
            //有些邮件服务器不加smtp.前缀
            SmtpClient client = new SmtpClient(from.Host);
            SendMail(client, from, mailPwd, to, message);
          
            return "发送邮件成功!包含附件:" + fileUrl + " 含截图?" + screen + "  " + DateTime.Now.ToString();
 
          }
          catch (SmtpException err)
          {
           
            return "发送邮件失败!" + err.Message + "  " + DateTime.Now.ToString();
          }
        }
        else
        {
         
          return "发送邮件失败!" + ex.Message + "  " + DateTime.Now.ToString();
        }
      }
    }
 
    //根据指定的参数发送邮件
    private static void SendMail(SmtpClient client, MailAddress from, string password,
       MailAddress to, MailMessage message)
    {
      //不使用默认凭证,注意此句必须放在client.Credentials的上面
      client.UseDefaultCredentials = false;
      //指定用户名、密码
      client.Credentials = new NetworkCredential(from.Address, password);
      //邮件通过网络发送到服务器
      client.DeliveryMethod = SmtpDeliveryMethod.Network;
      try
      {
        client.Send(message);
      }
      catch
      {
        throw;
      }
      finally
      {
        //及时释放占用的资源
        message.Dispose();
      }
    }
 
    #endregion

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/qc-id-01/p/7514316.html