I have always wanted to master the mail sending function, and I 've been dragging it all over the past two days. I 've finally looked at it and sorted it out. I hope it can help me learn it.
The SMTP server can be used to send emails. One is to use the SMTP function of IIS, and the other is to directly use the SMTP function of the mail supplier, such as Gmail, Sina, and QQ, to use these SMTP servers, you must register an account. Generally, you can directly use your mailbox and password. However, some mailboxes must enable the POP3/SMTP service. For example, QQ mail is disabled by default, you can find it in "Settings"> "account. I used the second method for sorting out today.
Early. the NET version uses System. web. the Mail class provides the function to send emails; version 2.0 introduces the System. net. mail class to replace System. web. mail, but I use System in WebForm. net. mail always triggers an exception, and then uses System. web. mail is successful. You can see the differences in my code; ASP. net mvc 3 provides the WebMail class for sending mails, making it easier.
Console programs, WPF, WebForm, and ASP. net mvc I tested it. The console program and WPF both use System. net. mail, WebForm, and ASP. net mvc can all use System. web. mail, while ASP. net mvc 3 is more convenient to use WebMail directly. Next I will post the Code separately.
Console program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail; // Add reference
namespace SendEmail
{
class Program
{
static void Main (string [] args)
{
Program p = new Program ();
bool flag = p.SendEmail ();
if (flag)
{
Console.Write ("Success!");
}
Console.Read ();
}
public bool SendEmail ()
{
MailMessage msg = new MailMessage ();
msg.To.Add ("1234567@qq.com"); // Mail recipient's account
msg.From = new MailAddress ("1234567@qq.com", "nickname", System.Text.Encoding.UTF8); // The account number and display name and character encoding for sending mail
msg.Subject = "Subject"; // E-mail subject
msg.SubjectEncoding = System.Text.Encoding.UTF8; // Email header encoding
msg.Body = "Content"; // Email content
msg.BodyEncoding = System.Text.Encoding.UTF8; // Email content encoding
msg.IsBodyHtml = false; // Whether it is HTML mail
msg.Priority = MailPriority.High; // Mail priority
SmtpClient client = new SmtpClient ();
client.Credentials = new System.Net.NetworkCredential ("1234567@qq.com", "password"); // The registered mailbox and password, as far as the QQ mailbox is concerned, if an independent password is set, the independent password should be used instead
client.Host = "smtp.qq.com"; // SMTP server corresponding to QQ mailbox
object userState = msg;
try
{
client.SendAsync (msg, userState);
return true;
}
catch (Exception ex)
{
Console.WriteLine (ex.Message);
return false;
}
}
}
}
WPF:
System.Net.Mail;
Button_Click( flag = MessageBox.Show(,
MailMessage message = message.To.Add( message.From = MailAddress(, message.Subject = message.SubjectEncoding = message.Body = message.BodyEncoding = message.IsBodyHtml = message.Priority = Attachment att = Attachment();
SmtpClient smtp = smtp.Credentials = System.Net.NetworkCredential(, smtp.Host = userState =
MessageBox.Show(ex.Message, }
WebForm:
System.Web.Mail;
Page_Load(
Button1_Click( flag = Response.Write(
MailMessage message = message.To = message.From = message.Subject = message.Body =
message.Fields.Add(,
message.Fields.Add(,
message.Fields.Add(,
SmtpMail.SmtpServer =
}
ASP. net mvc:
= = ;
WebMail.EnableSsl = ;
WebMail.UserName = = = ,,“Content