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  (未测试)

  • 相关阅读:
    Flash P2P 对象复制详解[转]
    解决flashbuilder控制台无输出一例
    评论:工程师们,不要想一辈子靠技术混饭吃[转]
    flashdevelop使用svn
    actionscript中this和super的指向问题
    使用Apparat框架优化你的Flash[转]
    robotlegs【转】
    Flash Player和Adobe AIR内部的垃圾回收机制[转]
    away3d4.0 基础demo一则
    使用JDBC进行批处理
  • 原文地址:https://www.cnblogs.com/margin-gu/p/7068993.html
Copyright © 2011-2022 走看看