• 大小: 39KB
    文件类型: .zip
    金币: 2
    下载: 1 次
    发布日期: 2021-06-12
  • 语言: C#
  • 标签: 示例demo  

资源简介

示例demo,含发送邮件的相关类源代码,分享一下。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.Configuration;

namespace SendEmailSample
{
    /// 
    /// 邮件帮助类
    /// 

    public static class MailHelper
    {
        /// 
        /// 发送邮件
        /// 

        /// 邮件内容对象
        /// 发送邮件所遇到的异常
        public static string SendMail(MailRequest request)
        {
            try
            {
                MailMessage mail = new MailMessage();

                if (string.IsNullOrEmpty(request.From))
                {
                    request.From = WebConfigurationManager.AppSettings[“DefaultMailFrom“];
                }
                mail.From = new MailAddress(request.From);

                PaserMailAddress(request.To mail.To);
                PaserMailAddress(request.CC mail.CC);
                PaserMailAddress(request.Bcc mail.Bcc);

                mail.Subject = request.Subject;
                mail.SubjectEncoding = System.Text.Encoding.UTF8;
                mail.Body = request.Body;
                mail.ReplyTo = new MailAddress(request.From);
                mail.IsBodyHtml = true;

                if (request.Attachments != null && request.Attachments.Length > 0)
                {
                    for (int i = 0; i < request.Attachments.Length; i++)
                    {
                        Attachment mailAttach = new Attachment(ByteArrayToStream(request.Attachments[i].FileData) request.Attachments[i].FileName);

                        mail.Attachments.Add(mailAttach);
                    }
                }

                if (string.IsNullOrEmpty(WebConfigurationManager.AppSettings[“SMTPSERVER“]))
                {
                    throw new ApplicationException(“邮件服务无效“);
                }

                //Smtp Server
                SmtpClient mailClient = new SmtpClient(WebConfigurationManager.AppSettings[“SMTPSERVER“]);

                if (!string.IsNullOrEmpty(WebConfigurationManager.AppSettings[“SMTPSERVERPORT“]))
                {
                    //端口号
                    try
                    {
                        mailClient.Port = Int32.Parse(WebConfigurationManager.AppSettings[“SMTPSERVERPORT“]);
                    }
                    catch
                    {
                        return “SMTP服务器端口设置错误,端口必须设置为数值型“;
                    }
                }

                if (!string.IsNullOrEmpty(WebConfigurationManager.AppSettings[“MAILUSER“]))
                {
                    mailClient.Credentials = new System.Net.NetworkCredential(WebConfigurationManager.AppSettings[“MAILUSER“] WebConfigurationManager.AppSettings[“MAILUSERPW“]);
                    mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                }
                else

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2013-08-04 21:20  SendEmailSample\
     目录           0  2013-08-04 21:22  SendEmailSample\SendEmailSample\
     文件         935  2013-08-04 21:20  SendEmailSample\SendEmailSample.sln
     文件       27648  2013-08-04 21:25  SendEmailSample\SendEmailSample.v11.suo
     目录           0  2013-08-04 21:22  SendEmailSample\SendEmailSample\bin\
     文件        9728  2013-08-04 21:23  SendEmailSample\SendEmailSample\bin\SendEmailSample.dll
     文件       28160  2013-08-04 21:23  SendEmailSample\SendEmailSample\bin\SendEmailSample.pdb
     目录           0  2013-08-04 21:21  SendEmailSample\SendEmailSample\doc\
     文件          70  2013-08-04 21:21  SendEmailSample\SendEmailSample\doc\test.txt
     文件        5045  2013-08-04 21:23  SendEmailSample\SendEmailSample\MailHelper.cs
     文件        3320  2013-08-04 21:22  SendEmailSample\SendEmailSample\MailRequest.cs
     文件        1162  2013-08-04 21:22  SendEmailSample\SendEmailSample\MailRequestAttachments.cs
     目录           0  2013-08-04 21:20  SendEmailSample\SendEmailSample\obj\
     目录           0  2013-08-04 21:23  SendEmailSample\SendEmailSample\obj\Debug\
     文件        7489  2013-08-04 21:22  SendEmailSample\SendEmailSample\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     文件         471  2013-08-04 21:22  SendEmailSample\SendEmailSample\obj\Debug\SendEmailSample.csproj.FileListAbsolute.txt
     文件        2247  2013-08-04 21:22  SendEmailSample\SendEmailSample\obj\Debug\SendEmailSample.csprojResolveAssemblyReference.cache
     文件        9728  2013-08-04 21:23  SendEmailSample\SendEmailSample\obj\Debug\SendEmailSample.dll
     文件       28160  2013-08-04 21:23  SendEmailSample\SendEmailSample\obj\Debug\SendEmailSample.pdb
     目录           0  2013-08-04 21:20  SendEmailSample\SendEmailSample\obj\Debug\TempPE\
     目录           0  2013-08-04 21:20  SendEmailSample\SendEmailSample\Properties\
     文件        1332  2013-08-04 21:20  SendEmailSample\SendEmailSample\Properties\AssemblyInfo.cs
     文件        5148  2013-08-04 21:22  SendEmailSample\SendEmailSample\SendEmailSample.csproj
     文件        1086  2013-08-04 21:22  SendEmailSample\SendEmailSample\SendEmailSample.csproj.user
     文件         455  2013-08-04 21:22  SendEmailSample\SendEmailSample\SendEmailTest.aspx
     文件        1987  2013-08-04 21:22  SendEmailSample\SendEmailSample\SendEmailTest.aspx.cs
     文件         811  2013-08-04 21:22  SendEmailSample\SendEmailSample\SendEmailTest.aspx.designer.cs
     文件        7741  2013-08-04 21:25  SendEmailSample\SendEmailSample\Web.config
     文件        1245  2013-08-04 21:20  SendEmailSample\SendEmailSample\Web.Debug.config
     文件        1306  2013-08-04 21:20  SendEmailSample\SendEmailSample\Web.Release.config

评论

共有 条评论

相关资源