码迷,mamicode.com
首页 > 其他好文 > 详细

调用SMTP发送有附件的邮件

时间:2015-07-30 23:04:14      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;
using System.Text.RegularExpressions;

public class EmailUtils
{
        #region  验证输入为Email
        /// <summary>
        /// 验证输入为Email
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool validateEmail(string str)
        {
            return Regex.IsMatch(str, @"\w+([-+.‘]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
        }
        #endregion

        public static void SendEmail(string txtSend,string txtTo)
        {
            try
            {
                if (validateEmail(txtSend))
                {
                    //设置邮件发送人和接收人
                    MailMessage message = null;
                    if (txtTo.IndexOf(",") != -1)
                    {
                        string[] strEmail = txtTo.Split(‘,‘);
                        string sumEmail = "";
                        for (int i = 0; i < strEmail.Length; i++)
                        {
                            sumEmail = strEmail[i];
                            message = new MailMessage(new MailAddress(txtSend), new MailAddress(sumEmail));
                            SendEmail(message);
                        }
                    }
                    else
                    {
                        message = new MailMessage(new MailAddress(txtSend), new MailAddress(txtTo));
                        SendEmail(message);
                    }
                    Log("发送成功");
                }
            }
            catch
            {
                Log("发送失败!");
            }
        }

        private void SendEmail(MailMessage message)
        {
            message.Subject = Base64Encode("testSubject");    //设置发送邮件的主题
            message.Body = Base64Encode("testContent");       //设置发送邮件的内容
            if (txtAttachment!= "")
            {
                if (txtAttachment.IndexOf(",") != 0)
                {
                    string[] strAttachment = txtAttachment.Split(‘,‘);
                    for (int i = 0; i < strAttachment.Length; i++)
                    {
                        AddFile(strAttachment[i], message);
                    }
                }
                else
                {
                    AddFile(txtAttachment, message);
                }
            }
            //实例化SmtpClient邮件发送类对象
            SmtpClient client = new SmtpClient(Dns.GetHostName(), Convert.ToInt32("25"));
            //设置用于验证发件人身份的凭据
            client.Credentials = new System.Net.NetworkCredential("MyTest", "*****");
            //发送邮件
            client.Send(message);
        }

        //对邮件内容进行编码
        private static string Base64Encode(string str)
        {
            return Convert.ToBase64String(Encoding.UTF8.GetBytes(str));
        }

        private void AddFile(string strFile,MailMessage message)
        {
            //为要发送的邮件创建附件信息
            Attachment myAttachment = new Attachment(strFile, System.Net.Mime.MediaTypeNames.Application.Octet);
            //为附件添加时间信息
            System.Net.Mime.ContentDisposition disposition = myAttachment.ContentDisposition;
            disposition.CreationDate = System.IO.File.GetCreationTime(strFile);
            disposition.ModificationDate = System.IO.File.GetLastWriteTime(strFile);
            disposition.ReadDate = System.IO.File.GetLastAccessTime(strFile);
            message.Attachments.Add(myAttachment);			//将创建的附件添加到邮件中
        }
}

 

调用SMTP发送有附件的邮件

标签:

原文地址:http://www.cnblogs.com/ziranquliu/p/4690756.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!