中央論壇 - CENTER BBS
標題:
c#用system.net.mail發信
[打印本頁]
作者:
f66666602
時間:
2007-8-14 05:04
標題:
c#用system.net.mail發信
using System;
using System.ComponentModel;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Windows.Forms;
namespace MyMail
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click_1(object sender, EventArgs e)
{
// Mail Message Setting
string fromEmail = "username@gmail.com";
string fromName = "username";
MailAddress from = new MailAddress(fromEmail, fromName, Encoding.UTF8);
string toEmail = "YYY@gmail.com";
MailMessage mail = new MailMessage(from, new MailAddress(toEmail));
string subject = "Test Subject";
mail.Subject = subject;
mail.SubjectEncoding = Encoding.UTF8;
string body = "Test Body";
mail.Body = body;
mail.BodyEncoding = Encoding.UTF8;
mail.IsBodyHtml = false;
mail.Priority = MailPriority.High;
// SMTP Setting
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.Credentials = new NetworkCredential("username@gmail.com", "password");
client.EnableSsl = true;
// Send Mail
client.SendAsync(mail, mail);
// Sent Compeleted Eevet
client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);
}
// Handle Sent Compeleted Eevet
private void client_SendCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show(e.Error.ToString());
}
else
{
MessageBox.Show("Message sent.");
}
}
}
}
複製代碼
歡迎光臨 中央論壇 - CENTER BBS (https://www.centerbbs.com/)
Powered by Discuz! X3