zoukankan      html  css  js  c++  java
  • c# 发送带附件的邮件

    using System;
    using System.IO;
    using System.Net;
    using System.Net.Mail;
    using System.Net.Mime;
    using System.Text;
    
    public class EmailUitls
        {
            /// <summary>
            /// 发送邮件
            /// </summary>
            /// <param name="fileName">附件路径</param>
            /// <param name="name">发件人名字</param>
            /// <param name="eamil">接收邮箱</param>
            /// <param name="title">标题</param>
            /// <param name="content">内容</param>
            /// <returns>成功/失败</returns>
            public static string send(string fileName, string name, string eamil, string title, string content)
            {
                SmtpClient client = new SmtpClient("smtp.qq.com");
                client.EnableSsl = true;
                client.UseDefaultCredentials = false;
                client.Credentials = new System.Net.NetworkCredential("发件人邮箱", "POP3/SMTP服务授权码");
    
                MailAddress from = new MailAddress("发件人邮箱", name, Encoding.UTF8);//初始化发件人  
    
                MailAddress to = new MailAddress(eamil, "", Encoding.UTF8);//初始化收件人  
    
                //设置邮件内容  
                MailMessage message = new MailMessage(from, to);
                message.Body = content.Trim();
                message.BodyEncoding = Encoding.UTF8;
                message.Subject = title.Trim();
                message.SubjectEncoding = Encoding.UTF8;
                message.IsBodyHtml = false;
    
                if (!string.IsNullOrEmpty(fileName))
                {
                    //附件
                    Attachment myAttachment = new System.Net.Mail.Attachment(fileName, MediaTypeNames.Application.Octet);
    
                    //MIME协议下的一个对象,用以设置附件的创建时间,修改时间以及读取时间  
                    ContentDisposition disposition = myAttachment.ContentDisposition;
                    disposition.CreationDate = File.GetCreationTime(fileName);
                    disposition.ModificationDate = File.GetLastWriteTime(fileName);
                    disposition.ReadDate = File.GetLastAccessTime(fileName);
    
                    //用smtpclient对象里attachments属性,添加上面设置好的myattachment  
                    message.Attachments.Add(myAttachment);
                }
    
                try
                {
                    client.Send(message);
                    return "邮件发送成功!";
                }
                catch (Exception ex)
                {
                    return "邮件发送失败!" + ex.Message;
                }  
            }
        }

     有一个问题,附件太大会发送失败 

     解决方法:https://support.microsoft.com/zh-cn/kb/2468871  (未测试)

  • 相关阅读:
    指针
    《算法图解》3
    《算法图解》2
    Python缩进
    linux_01_seq_shuf_sort
    centos yum 切换阿里源
    Python csv 跳过第一行 去除表头 读取文件内容
    C语言结构体内存分析
    shell csv导入mysql ERROR 1292 (22007) at line 1: Incorrect datetime value: '' for column 解决方案
    代码实现:数据结构之数组当中唯一的k个数 k=1,2
  • 原文地址:https://www.cnblogs.com/margin-gu/p/7068993.html
Copyright © 2011-2022 走看看