smtp发邮件的类

 

这是一个可以发送Smtp的一个类,.Net Framework2.0 中的 System.Web.Mail 来开发虽然此命名空间中的类已被否决。但我就是它来开发的,System.Net.Mail 老是报错,只能用英文。所以我也是被逼的啊!!!!

类如下

  1 None.gif using  System;
  2 None.gif using  System.Collections.Generic;
  3 None.gif using  System.Text;
  4 None.gif using  System.Collections;
  5 None.gif using  System.Web.Mail;
  6 None.gif
  7 None.gif namespace  MyClassLibrary
  8 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  9InBlock.gif
 10InBlock.gif    public class MySmtpMail
 11ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 12ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 13InBlock.gif        /// 指定电子邮件具de优先级
 14ExpandedSubBlockEnd.gif        /// </summary>

 15InBlock.gif        public enum emailPriorityenum
 16ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 17ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//// <summary>
 18InBlock.gif            /// 指定电子邮件具有高优先级
 19ExpandedSubBlockEnd.gif            /// </summary>

 20InBlock.gif            High = MailPriority.High,
 21ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//// <summary>
 22InBlock.gif            /// 指定电子邮件具有低优先级
 23ExpandedSubBlockEnd.gif            /// </summary>

 24InBlock.gif            Low = MailPriority.Low,
 25ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//// <summary>
 26InBlock.gif            /// 指定电子邮件具有普通优先级
 27ExpandedSubBlockEnd.gif            /// </summary>

 28InBlock.gif            Normal = MailPriority.Normal
 29ExpandedSubBlockEnd.gif        }

 30ContractedSubBlock.gifExpandedSubBlockStart.gif        属性#region 属性
 31InBlock.gif        string emailServerhost ="smtp.qq.com";
 32ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 33InBlock.gif        /// 设置用于发送电子邮件的Smtp服务器地址
 34ExpandedSubBlockEnd.gif        /// </summary>

 35InBlock.gif        public string EmailServerhost
 36ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 37ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn emailServerhost; }
 38ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ emailServerhost = value; }
 39ExpandedSubBlockEnd.gif        }

 40InBlock.gif        string emailUserpassword = "123456";
 41ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 42InBlock.gif        /// 设置发送电子邮件的用户密码
 43ExpandedSubBlockEnd.gif        /// </summary>

 44InBlock.gif        public string EmailUserpassword
 45ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 46ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ emailUserpassword = value; }
 47ExpandedSubBlockEnd.gif        }

 48InBlock.gif        string emailUserName = "330514962@qq.com";
 49ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 50InBlock.gif        ///   获取或设置发送电子邮件的用户名
 51ExpandedSubBlockEnd.gif        /// </summary>

 52InBlock.gif        public string EmailUserName
 53ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 54ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn emailUserName; }
 55ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ emailUserName = value; }
 56ExpandedSubBlockEnd.gif        }

 57InBlock.gif
 58InBlock.gif        string fromemail = "330514962@qq.com";
 59ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 60InBlock.gif        /// 获取或设置发信人的电子邮件地址
 61ExpandedSubBlockEnd.gif        /// </summary>

 62InBlock.gif        public string Fromemail
 63ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 64ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn fromemail; }
 65ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ fromemail = value; }
 66ExpandedSubBlockEnd.gif        }

 67InBlock.gif
 68InBlock.gif        string emailto = "1029589450@qq.com";//"330514962@qq.com";
 69ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 70InBlock.gif        /// 获取或设置收件者邮箱多个以逗号隔开
 71ExpandedSubBlockEnd.gif        /// </summary>

 72InBlock.gif        public string Emailto
 73ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 74ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn emailto; }
 75ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ emailto = value; }
 76ExpandedSubBlockEnd.gif        }

 77InBlock.gif        string emailSubject = string.Empty;
 78ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 79InBlock.gif        /// 获取或设置电子邮件的主题
 80ExpandedSubBlockEnd.gif        /// </summary>

 81InBlock.gif        public string EmailSubject
 82ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 83ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn emailSubject; }
 84ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ emailSubject = value; }
 85ExpandedSubBlockEnd.gif        }

 86InBlock.gif        MailPriority emailPriority = MailPriority.Normal;
 87ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 88InBlock.gif        /// 获取或设置电子邮件的优先级
 89ExpandedSubBlockEnd.gif        /// </summary>

 90InBlock.gif        public emailPriorityenum EmailPriority
 91ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 92InBlock.gif            get
 93ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 94InBlock.gif                if (emailPriority == MailPriority.High)
 95ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 96InBlock.gif                    return emailPriorityenum.High;
 97ExpandedSubBlockEnd.gif                }

 98InBlock.gif                else if (emailPriority == MailPriority.Low)
 99ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
100InBlock.gif                    return emailPriorityenum.Low;
101ExpandedSubBlockEnd.gif                }

102InBlock.gif                else
103ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
104InBlock.gif                    return emailPriorityenum.Normal;
105ExpandedSubBlockEnd.gif                }

106ExpandedSubBlockEnd.gif            }

107ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ emailPriority = (MailPriority)value; }
108ExpandedSubBlockEnd.gif        }

109InBlock.gif
110InBlock.gif        static string emailACopyUser;
111ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
112InBlock.gif        ///  获取或设置邮件抄送人,多个以逗号隔开
113ExpandedSubBlockEnd.gif        /// </summary>

114InBlock.gif        public string EmailACopyUser
115ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
116ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn emailACopyUser; }
117ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ emailACopyUser = value; }
118ExpandedSubBlockEnd.gif        }

119InBlock.gif        Encoding bodyEncoding = Encoding.UTF8;
120ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
121InBlock.gif        ///  获取或设置邮件的编码
122ExpandedSubBlockEnd.gif        /// </summary>

123InBlock.gif        public Encoding BodyEncoding
124ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
125ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn bodyEncoding; }
126ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ bodyEncoding = value; }
127ExpandedSubBlockEnd.gif        }

128InBlock.gif
129InBlock.gif        string emailAttachment;
130ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
131InBlock.gif        /// 获取或设置邮件所发送的附件的路径,多个以逗号隔开
132ExpandedSubBlockEnd.gif        /// </summary>

133InBlock.gif        public string EmailAttachment
134ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
135ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn emailAttachment; }
136ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ emailAttachment = value; }
137ExpandedSubBlockEnd.gif        }

138InBlock.gif
139InBlock.gif        string emailBody = string.Empty;
140ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
141InBlock.gif        /// 获取或设置将要发送的邮件的正文
142ExpandedSubBlockEnd.gif        /// </summary>

143InBlock.gif        public string EmailBody
144ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
145ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn emailBody; }
146ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ emailBody = value; }
147ExpandedSubBlockEnd.gif        }

148ExpandedSubBlockEnd.gif        #endregion

149InBlock.gif
150InBlock.gif
151ContractedSubBlock.gifExpandedSubBlockStart.gif        邮件发送构造函数#region 邮件发送构造函数
152InBlock.gif
153ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
154InBlock.gif        /// 邮件发送
155ExpandedSubBlockEnd.gif        /// </summary>

156InBlock.gif        public MySmtpMail()
157ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
158InBlock.gif
159ExpandedSubBlockEnd.gif        }

160InBlock.gif
161ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
162InBlock.gif        /// 邮件发送
163InBlock.gif        /// </summary>
164ExpandedSubBlockEnd.gif        /// <param name="NewEmailBody">设置邮件的正文</param>

165InBlock.gif        public MySmtpMail(string NewEmailBody)
166ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
167InBlock.gif            EmailBody = NewEmailBody;
168ExpandedSubBlockEnd.gif        }

169ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
170InBlock.gif        /// 邮件发送
171InBlock.gif        /// </summary>
172InBlock.gif        /// <param name="NewEmailBody">设置邮件的正文</param>
173ExpandedSubBlockEnd.gif        /// <param name="To">收件者邮箱多个以逗号隔开</param>

174InBlock.gif        public MySmtpMail(string NewEmailBody, string To)
175ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
176InBlock.gif            EmailBody = NewEmailBody;
177InBlock.gif            Emailto = To;
178ExpandedSubBlockEnd.gif        }

179ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
180InBlock.gif        /// 邮件发送
181InBlock.gif        /// </summary>
182InBlock.gif        /// <param name="To">收件者邮箱多个以逗号隔开</param>
183ExpandedSubBlockEnd.gif        /// <param name="emailAttachment">邮件所发送的附件的路径,多个以逗号隔开</param>

184InBlock.gif        public MySmtpMail(string NewEmailBody, string To, string newemailAttachment)
185ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
186InBlock.gif            EmailBody = NewEmailBody;
187InBlock.gif            Emailto = To;
188InBlock.gif            EmailAttachment = newemailAttachment;
189ExpandedSubBlockEnd.gif        }

190ExpandedSubBlockEnd.gif        #endregion

191InBlock.gif
192ContractedSubBlock.gifExpandedSubBlockStart.gif        发送E-Mail#region 发送E-Mail
193ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
194InBlock.gif        ///  发送邮件
195InBlock.gif        /// </summary>
196ExpandedSubBlockEnd.gif        /// <returns>如果返回为真 , 标示邮件发送成功,否则为失败</returns>

197InBlock.gif        public bool SendMessage()
198ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
199InBlock.gif            MailMessage message = new MailMessage();
200InBlock.gif            message.From = Fromemail;
201InBlock.gif            message.Body = EmailBody;
202InBlock.gif            message.BodyEncoding = BodyEncoding;
203InBlock.gif            message.Cc = EmailACopyUser;
204InBlock.gif            message.Priority = emailPriority;
205InBlock.gif            message.Subject = EmailSubject;
206InBlock.gif            message.To = Emailto;
207InBlock.gif            message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate""1");
208InBlock.gif            message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", EmailUserName);
209InBlock.gif            message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", emailUserpassword);
210InBlock.gif            if (EmailAttachment != null && EmailAttachment.Trim()!="")
211ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
212ExpandedSubBlockStart.gifContractedSubBlock.gif                char[] delim = new char[] dot.gif',' };
213InBlock.gif                foreach (string att in EmailAttachment.Trim().Split(delim))
214ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
215InBlock.gif                    MailAttachment myAttachment = new MailAttachment(att);
216InBlock.gif                    message.Attachments.Add(myAttachment);
217ExpandedSubBlockEnd.gif                }

218ExpandedSubBlockEnd.gif            }

219InBlock.gif            try
220ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
221InBlock.gif                SmtpMail.SmtpServer = EmailServerhost;
222InBlock.gif                SmtpMail.Send(message);
223ExpandedSubBlockEnd.gif            }

224InBlock.gif            catch (System.Web.HttpException ehttp)
225ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
226InBlock.gif                return false;
227ExpandedSubBlockEnd.gif            }

228InBlock.gif            return true;
229ExpandedSubBlockEnd.gif        }

230ExpandedSubBlockEnd.gif        #endregion

231ExpandedSubBlockEnd.gif    }

232ExpandedBlockEnd.gif}

写的很垃圾 , 还望各位高手些指点下

转载于:https://www.cnblogs.com/hongshao/articles/1537003.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值