这里用的是新浪邮箱,首先申请个邮箱,登录后,在“邮箱设置”里面设置下,否则发送的时候会报错!
打开邮箱设置--》账户,POP/SMTP设置,把“开启”勾上。
代码:
- using System.Net.Mail;
- protected void ButtonSend_Click(object sender, EventArgs e)
- {
- string from = "发件人邮箱
- string subject = "测试";
- string body = "test 测试,by C#,胡千好";
- //收件人邮箱,多个邮箱用逗号分隔
- string to = "";
- MailMessage mailMessage = new MailMessage(from, to, subject.ToString(), body.ToString());
- this.sendMail(mailMessage);
- }
- private void sendMail(MailMessage mail)
- {
- SmtpClient smtpClient = new SmtpClient();
- //smtpClient.EnableSsl = true;
- smtpClient.Host = "smtp.sina.com";
- //发件人邮箱和密码
- smtpClient.Credentials = new NetworkCredential(mail.From.Address, "*******");
- try
- {
- smtpClient.Send(mail);
- Response.Write("发送成功");
- }
- catch(Exception e)
- {
- Response.Write("出错啦" + e.StackTrace.ToString());
- }
- }
转载于:https://blog.51cto.com/huqianhao/956560