邮箱发送

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net.Mime;
using System.IO;
using System.Timers;
using System.Xml;

namespace MyEmailTest
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //smtp.163.com
                string senderServerIp = "123.125.50.133";
                //smtp.gmail.com
                //string senderServerIp = "74.125.127.109";
                //smtp.qq.com
                //string senderServerIp = "58.251.149.147";
                //string senderServerIp = "smtp.sina.com";
                string toMailAddress = "[email protected]";
                string fromMailAddress = "[email protected]";
                string subjectInfo = "Test sending e_mail";
                string bodyInfo = "Hello Eric, This is my first testing e_mail";
                string mailUsername = "mingmingruyuedlut";
                string mailPassword = ".........."; //发送邮箱的密码()
                string mailPort = "25";
                string attachPath = "E:\\123123.txt; E:\\haha.pdf";

                MyEmail email = new MyEmail(senderServerIp, toMailAddress, fromMailAddress, subjectInfo, bodyInfo, mailUsername, mailPassword, mailPort, false, false);
                email.AddAttachments(attachPath);
                email.Send();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }

    public class MyEmail
    {
        private MailMessage mMailMessage;   //主要处理发送邮件的内容(如:收发人地址、标题、主体、图片等等)
        private SmtpClient mSmtpClient; //主要处理用smtp方式发送此邮件的配置信息(如:邮件服务器、发送端口号、验证方式等等)
        private int mSenderPort;   //发送邮件所用的端口号(htmp协议默认为25)
        private string mSenderServerHost;    //发件箱的邮件服务器地址(IP形式或字符串形式均可)
        private string mSenderPassword;    //发件箱的密码
        private string mSenderUsername;   //发件箱的用户名(即@符号前面的字符串,例如:[email protected],用户名为:hello)
        private bool mEnableSsl;    //是否对邮件内容进行socket层加密传输
        private bool mEnablePwdAuthentication;  //是否对发件人邮箱进行密码验证

        ///<summary>
        /// 构造函数
        ///</summary>
        ///<param name="server">发件箱的邮件服务器地址</param>
        ///<param name="toMail">收件人地址(可以是多个收件人,程序中是以“;"进行区分的)</param>
        ///<param name="fromMail">发件人地址</param>
        ///<param name="subject">邮件标题</param>
        ///<param name="emailBody">邮件内容(可以以html格式进行设计)</param>
        ///<param name="username">发件箱的用户名(即@符号前面的字符串,例如:[email protected],用户名为:hello)</param>
        ///<param name="password">发件人邮箱密码</param>
        ///<param name="port">发送邮件所用的端口号(htmp协议默认为25)</param>
        ///<param name="sslEnable">true表示对邮件内容进行socket层加密传输,false表示不加密</param>
        ///<param name="pwdCheckEnable">true表示对发件人邮箱进行密码验证,false表示不对发件人邮箱进行密码验证</param>
        public MyEmail(string server, string toMail, string fromMail, string subject, string emailBody, string username, string password, string port, bool sslEnable, bool pwdCheckEnable)
        {
            try
            {
                mMailMessage = new MailMessage();
                mMailMessage.To.Add(toMail);
                mMailMessage.From = new MailAddress(fromMail);
                mMailMessage.Subject = subject;
                mMailMessage.Body = emailBody;
                mMailMessage.IsBodyHtml = true;
                mMailMessage.BodyEncoding = System.Text.Encoding.UTF8;
                mMailMessage.Priority = MailPriority.Normal;
                this.mSenderServerHost = server;
                this.mSenderUsername = username;
                this.mSenderPassword = password;
                this.mSenderPort = Convert.ToInt32(port);
                this.mEnableSsl = sslEnable;
                this.mEnablePwdAuthentication = pwdCheckEnable;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        ///<summary>
        /// 添加附件
        ///</summary>
        ///<param name="attachmentsPath">附件的路径集合,以分号分隔</param>
        public void AddAttachments(string attachmentsPath)
        {
            try
            {
                string[] path = attachmentsPath.Split(‘;‘); //以什么符号分隔可以自定义
                Attachment data;
                ContentDisposition disposition;
                for (int i = 0; i < path.Length; i++)
                {
                    data = new Attachment(path[i], MediaTypeNames.Application.Octet);
                    disposition = data.ContentDisposition;
                    disposition.CreationDate = File.GetCreationTime(path[i]);
                    disposition.ModificationDate = File.GetLastWriteTime(path[i]);
                    disposition.ReadDate = File.GetLastAccessTime(path[i]);
                    mMailMessage.Attachments.Add(data);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        ///<summary>
        /// 邮件的发送
        ///</summary>
        public void Send()
        {
            try
            {
                if (mMailMessage != null)
                {
                    mSmtpClient = new SmtpClient();
                    //mSmtpClient.Host = "smtp." + mMailMessage.From.Host;
                    mSmtpClient.Host = this.mSenderServerHost;
                    mSmtpClient.Port = this.mSenderPort;
                    mSmtpClient.UseDefaultCredentials = false;
                    mSmtpClient.EnableSsl = this.mEnableSsl;
                    if (this.mEnablePwdAuthentication)
                    {
                        System.Net.NetworkCredential nc = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
                        //mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
                        //NTLM: Secure Password Authentication in Microsoft Outlook Express
                        mSmtpClient.Credentials = nc.GetCredential(mSmtpClient.Host, mSmtpClient.Port, "NTLM");
                    }
                    else
                    {
                        mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
                    }
                    mSmtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                    mSmtpClient.Send(mMailMessage);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}
时间: 2024-11-25 21:13:23

邮箱发送的相关文章

asp.net中邮箱发送

邮箱发送今天终于解决了,从不会到会用了3个晚上才终于解决了,有好多问题都不是代码的问题,而是邮箱的设置上的问题.下面我一一的讲解一下. 1.邮箱发送的原理,我使用图片来解释 左边的[email protected]是发送的邮箱(下面我就是用a邮箱指代),右边的[email protected]是接收的邮箱(下面我就是用b邮箱指代). 1).邮箱a发送到他自己的smtp服务器上,如:邮箱a是outlook上注册的邮箱,那么邮箱a的邮件就发送到outlook上的smtp服务器上 2).通过smtp服

首次沟通邮箱发送技巧

您是否需要经常给一些新客户发送一些公司资料.产品介绍等? 是否遇到发送给新用户的邮件被归档到对方垃圾箱甚至被对方服务器拒收的尴尬? 本期,我们总结了一些常见的可能导致增加邮件被误判的情况,希望您了解并与新客户的沟通更加顺畅! 1. 不要选用复杂的签名: 通过企业邮箱发送商务邮件,基本都会使用到邮件签名或电子名片,并且很多用户会在签名中携带公司的各种产品介绍,网站链接,宣传图片,多种联系方式等等,但是这些都增加了邮件被收件方网关系统误判为垃圾邮件的风险,因此请在首次发信给新客户时,使用简洁的签名.

Smokeping 使用外部邮箱发送告警邮件

Smokeping 发送告警邮件 转自:http://www.humen1.net/2013/11/669 smokeping 默认用sendmail发邮件,这样不好. 改了一下源码 这样可以使用 我QQ的smtp server来发告警邮件了 首先需要安装 Authen::SASL 模块(auth 需要用的) 我用CPAN装的,不细说了 修改 smokeping/lib/Smokeping.pm 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17

EMS查看及修改邮箱发送和接受邮件大小的方法

默认情况下,新建用户邮箱没有进行单独设置,故用户邮箱默认值为"Unlimited"(未限制),即遵从全局设置(继承邮箱数据库策略).通过EMS查看用户邮箱发送和接受邮件大小的默认值,键入以下命令. Get-Mailbox wsj | Fl MaxsendSize,MaxReceiveSize (2)EMS限制用户发送和接受邮件大小(以用户"wsj"限制邮件发送和接受大小20mb为例).键入以下命令. Set-Mailbox wsj -MaxSendSize 20mb

通过java给qq邮箱发送信息

通过java程序给qq邮箱发送信息. 1.第一步:下载mail的jar包:javax.mail.jar 下载地址:https://javaee.github.io/javamail/#Latest_News 2.直接使用下面的代码 public static void sendMessages() { try { //创建Properties 类用于记录邮箱的一些属性 final Properties props = new Properties(); //表示SMTP发送邮件,必须进行身份验证

SpringBoot中快速实现邮箱发送

前言 在许多企业级项目中,需要用到邮件发送的功能,如: 注册用户时需要邮箱发送验证 用户生日时发送邮件通知祝贺 发送邮件给用户等 创建工程导入依赖 <!-- 邮箱发送依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> &l

随机验证码、图片验证码和邮箱发送用户验证码

随机验证码.图片验证码和邮箱发送用户验证码 一.随机验证码 随机验证码的生成比较简单一般在注册用户的时候与邮箱或者手机信息接口相结合实现发送验证码功能,随机验证码只需要使用python内置的random随机数函数,调用random模块:import random,具体实现代码块 #随机验证码 def authCode(): code = '' for i in range(6): current = random.randrange(0,6)#randrange随机生成0-6的数字,但不包括6

django 邮箱发送

在django中提供了邮件接口 QQ邮箱配置 qq邮箱地扯:https://mail.qq.com settings文件 # 邮箱配置 EMAIL_USE_SSL = True EMAIL_HOST = 'smtp.qq.com' # 如果是 163 改成 smtp.163.com EMAIL_PORT = 465 EMAIL_HOST_USER = '[email protected]' # 配置邮箱 EMAIL_HOST_PASSWORD = 'xxxxx' # 对应的授权码 DEFAULT

YII2.0邮箱发送

打开配置文件将下面代码添加到 components => [...]中(例:高级版默认配置在/common/config/main-local.php) 1 2 3 4 5 6 7 8 9 10 11 12 13         'mailer' => [             'class' => 'yii\swiftmailer\Mailer',             'viewPath' => '@common/mail',             'useFileTra