using System;using System.Net;using System.Net.Mail;
namespace SendMailViaGmail{class Program{static void Main(string[] args){
//Specify senders gmail addressstring SendersAddress = "Sendersaddress@gmail.com";//Specify The Address You want to sent Email To(can be any valid email address)string ReceiversAddress = "ReceiversAddress@yahoo.com";//Specify The password of gmial account u are using to sent mail(pw of sender@gmail.com)const string SendersPassword = "Password";//Write the subject of ur mailconst string subject = "Testing";//Write the contents of your mailconst string body = "Hi This Is my Mail From Gmail";
try{//we will use Smtp client which allows us to send email using SMTP Protocol//i have specified the properties of SmtpClient smtp within{}//gmails smtp server name is smtp.gmail.com and port number is 587SmtpClient smtp = new SmtpClient{Host = "smtp.gmail.com",Port = 587,EnableSsl = true,DeliveryMethod = SmtpDeliveryMethod.Network,Credentials = new NetworkCredential(SendersAddress, SendersPassword),Timeout = 3000};
//MailMessage represents a mail message//it is 4 parameters(From,TO,subject,body)
MailMessage message = new MailMessage(SendersAddress, ReceiversAddress, subject, body);/*WE use smtp sever we specified above to send the message(MailMessage message)*/
smtp.Send(message);Console.WriteLine("Message Sent Successfully");Console.ReadKey();}
catch (Exception ex){Console.WriteLine(ex.Message);Console.ReadKey();}}}}
using System.Net;using System.Net.Mail;
public void email_send(){MailMessage mail = new MailMessage();SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");mail.From = new MailAddress("your mail@gmail.com");mail.To.Add("to_mail@gmail.com");mail.Subject = "Test Mail - 1";mail.Body = "mail with attachment";
System.Net.Mail.Attachment attachment;attachment = new System.Net.Mail.Attachment("c:/textfile.txt");mail.Attachments.Add(attachment);
SmtpServer.Port = 587;SmtpServer.Credentials = new System.Net.NetworkCredential("your mail@gmail.com", "your password");SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
public void SendEmail(string address, string subject, string message){Thread threadSendMails;threadSendMails = new Thread(delegate(){
//Place your Code here
});threadSendMails.IsBackground = true;threadSendMails.Start();}
// Include this.using System.Net.Mail;
string fromAddress = "xyz@gmail.com";string mailPassword = "*****"; // Mail id password from where mail will be sent.string messageBody = "Write the body of the message here.";
// Create smtp connection.SmtpClient client = new SmtpClient();client.Port = 587;//outgoing port for the mail.client.Host = "smtp.gmail.com";client.EnableSsl = true;client.Timeout = 10000;client.DeliveryMethod = SmtpDeliveryMethod.Network;client.UseDefaultCredentials = false;client.Credentials = new System.Net.NetworkCredential(fromAddress, mailPassword);
// Fill the mail form.var send_mail = new MailMessage();
send_mail.IsBodyHtml = true;//address from where mail will be sent.send_mail.From = new MailAddress("from@gmail.com");//address to which mail will be sent.send_mail.To.Add(new MailAddress("to@example.com");//subject of the mail.send_mail.Subject = "put any subject here";
send_mail.Body = messageBody;client.Send(send_mail);
using (MailMessage mail = new MailMessage()){mail.From = new MailAddress("email@gmail.com");mail.To.Add("somebody@domain.com");mail.Subject = "Hello World";mail.Body = "<h1>Hello</h1>";mail.IsBodyHtml = true;mail.Attachments.Add(new Attachment("C:\\file.zip"));
using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587)){smtp.Credentials = new NetworkCredential("email@gmail.com", "password");smtp.EnableSsl = true;smtp.Send(mail);}}
public static void SendMail2Step(string SMTPServer, int SMTP_Port, string From, string Password, string To, string Subject, string Body, string[] FileNames) {var smtpClient = new SmtpClient(SMTPServer, SMTP_Port) {DeliveryMethod = SmtpDeliveryMethod.Network,UseDefaultCredentials = false,EnableSsl = true};smtpClient.Credentials = new NetworkCredential(From, Password); //Use the new password, generated from google!var message = new System.Net.Mail.MailMessage(new System.Net.Mail.MailAddress(From, "SendMail2Step"), new System.Net.Mail.MailAddress(To, To));smtpClient.Send(message);}
像这样使用:
SendMail2Step("smtp.gmail.com", 587, "youraccount@gmail.com","yjkjcipfdfkytgqv",//This will be generated by google, copy it here."recipient@barcodes.bg", "test message subject", "Test message body ...", null);
using System;using System.Net;using System.Net.Mail;
namespace SendMailViaGmail{class Program{static void Main(string[] args){
//Specify senders gmail addressstring SendersAddress = "Sendersaddress@gmail.com";//Specify The Address You want to sent Email To(can be any valid email address)string ReceiversAddress = "ReceiversAddress@yahoo.com";//Specify The password of gmial account u are using to sent mail(pw of sender@gmail.com)const string SendersPassword = "Password";//Write the subject of ur mailconst string subject = "Testing";//Write the contents of your mailconst string body = "Hi This Is my Mail From Gmail";
try{//we will use Smtp client which allows us to send email using SMTP Protocol//i have specified the properties of SmtpClient smtp within{}//gmails smtp server name is smtp.gmail.com and port number is 587SmtpClient smtp = new SmtpClient{Host = "smtp.gmail.com",Port = 587,EnableSsl = true,DeliveryMethod = SmtpDeliveryMethod.Network,Credentials = new NetworkCredential(SendersAddress, SendersPassword),Timeout = 3000};
//MailMessage represents a mail message//it is 4 parameters(From,TO,subject,body)
MailMessage message = new MailMessage(SendersAddress, ReceiversAddress, subject, body);/*WE use smtp sever we specified above to send the message(MailMessage message)*/
smtp.Send(message);Console.WriteLine("Message Sent Successfully");Console.ReadKey();}catch (Exception ex){Console.WriteLine(ex.Message);Console.ReadKey();}}}}
private void button1_Click(object sender, EventArgs e){try{MailMessage mail = new MailMessage();SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("your_email_address@gmail.com");mail.To.Add("to_address");mail.Subject = "Test Mail";mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 587;SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);MessageBox.Show("mail Send");}catch (Exception ex){MessageBox.Show(ex.ToString());}}
MimeMessage message = new MimeMessage();message.From.Add(new MailboxAddress("FromName", "YOU_FROM_ADDRESS@gmail.com"));message.To.Add(new MailboxAddress("ToName", "YOU_TO_ADDRESS@gmail.com"));message.Subject = "MyEmailSubject";
message.Body = new TextPart("plain"){Text = @"MyEmailBodyOnlyTextPart"};
using (var client = new SmtpClient()){client.Connect("SERVER", 25); // 25 is port you can change accordingly
// Note: since we don't have an OAuth2 token, disable// the XOAUTH2 authentication mechanism.client.AuthenticationMechanisms.Remove("XOAUTH2");
// Note: only needed if the SMTP server requires authenticationclient.Authenticate("YOUR_USER_NAME", "YOUR_PASSWORD");
client.Send(message);client.Disconnect(true);}
public static void SendMailFromApp(string SMTPServer, int SMTP_Port, string From, string Password, string To, string Subject, string Body) {var smtpClient = new SmtpClient(SMTPServer, SMTP_Port) {DeliveryMethod = SmtpDeliveryMethod.Network,UseDefaultCredentials = false,EnableSsl = true};smtpClient.Credentials = new NetworkCredential(From, Password); //Use the new password, generated from google!var message = new System.Net.Mail.MailMessage(new System.Net.Mail.MailAddress(From, "SendMail2Step"), new System.Net.Mail.MailAddress(To, To));smtpClient.Send(message);}
您可以像下面这样调用方法
SendMailFromApp("smtp.gmail.com", 25, "mygmailaccount@gmail.com","tyugyyj1556jhghg",//This will be generated by google, copy it here."mailme@gmail.com", "New Mail Subject", "Body of mail from My App");