chantroitinhoc

Joined : 10 Jan 2008 Posts : 24
| Subject: MAIL PROGRAM C# 15/5/2008, 11:24 | |
| Code chương trình C# send mail SMTP:
| Code: | using System; using System.Net.Mail;
class MailAttachTest { public static void Main() { Console.WriteLine("This is exercise 13.3"); //Create mail message MailMessage mail = new MailMessage();
//Set the address Console.WriteLine("Enter your email address: "); String from = Console.ReadLine(); Console.WriteLine("Enter the email address you want to send this message:"); String to = Console.ReadLine(); mail.From = new MailAddress(from); mail.To.Add(to);
//Set the contents mail.Subject = "This is an email."; mail.Headers.Add("Comments", "This message attempts to send a binary attachment"); mail.Body = "Here is a test file for you to try"; mail.Priority = MailPriority.High;
//Add an attachment from you system files. Console.WriteLine("Input attach file path:"); String attach = Console.ReadLine(); mail.Attachments.Add(new Attachment(attach));
//Send the message try { Console.WriteLine("Enter Mail Server IP address:"); String IP = Console.ReadLine(); SmtpClient smtp = new SmtpClient(IP); smtp.Send(mail); Console.WriteLine("Your message has been sent."); } catch (SmtpException) { Console.WriteLine("There were some errors."); } } }
|
_________________ A person who never made a mistake never tried anything new! http://www.echip.com.vn http://www.pcworld.com.vn http://www.khoahocphothong.com.vn
Last edited by chantroitinhoc on 23/5/2008, 11:57; edited 1 time in total |
|
chantroitinhoc

Joined : 10 Jan 2008 Posts : 24
| Subject: Re: MAIL PROGRAM C# 16/5/2008, 10:03 | |
| 1. Code for program: SMTP_Send_Mail (SendMail.cs)
| Code: | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;
using System.Net.Mail;
namespace SendMail { public partial class SendMail : Form { public SendMail() { InitializeComponent(); }
private void sendit_Click(object sender, EventArgs e) { string Content = body.Text; string Subject = subject.Text; string fromAddress = from.Text; string toAddress = to.Text; MailMessage mailMessage = new MailMessage( fromAddress, toAddress, Subject, Content); if (cc.Text != "") { mailMessage.CC.Add(cc.Text); } if (subject.Text != "") { mailMessage.Subject = subject.Text; }
if (path.Text != "") { mailMessage.Attachments.Add(new Attachment(path.Text)); } try { SmtpClient mailSender = new SmtpClient(IPadress.Text); //use this if you are in the development server mailSender.Send(mailMessage); IPadress.Text = ""; body.Text = ""; subject.Text = ""; from.Text = ""; to.Text = ""; cc.Text = ""; path.Text = ""; results.Items.Add("Your message has been sent."); } catch (SmtpException) { results.Items.Add("There were some errors when trying send mail."); } }
private void receive_Click(object sender, EventArgs e) { System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc.EnableRaisingEvents = false; proc.StartInfo.FileName = "PopCheck.exe"; proc.Start(); } } }
|
Check send mail: - Cài đặt Mail Server, ví dụ: cài MailDaemon lên máy có IP 192.168.1.3/24 - Tạo các tài khoản mail với user: admin, u1, u2 và password trùng với user cho dễ nhớ để test - Chạy SendMail.exe từ một máy khác để test.
2. Code for program: Receive_Mail_POP3_Client (PopCheck.cs)
| Code: | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;
using System.Net; using System.Net.Sockets; using System.Threading; using System.IO;
namespace PopCheck { public partial class PopCheck : Form { private TcpClient mailclient; private NetworkStream ns; private StreamReader sr; private StreamWriter sw; public PopCheck() { InitializeComponent(); results.DoubleClick += new EventHandler(getmessages_DoubleClick); }
private void login_Click(object sender, EventArgs e) { status.Text = "Checking for messages..."; Thread startlogin = new Thread(new ThreadStart(loginandretr)); startlogin.IsBackground = true; startlogin.Start();
}
private void close_Click(object sender, EventArgs e) { if (ns != null) { sw.Close(); sr.Close(); ns.Close(); mailclient.Close(); } Close();
} void loginandretr() { string response; string from = null; string subject = null; int totmessages; try { mailclient = new TcpClient(hostname.Text, 110); } catch (SocketException) { status.Text = "Unable to connect to server"; return; } ns = mailclient.GetStream(); sr = new StreamReader(ns); sw = new StreamWriter(ns); response = sr.ReadLine(); //Get opening POP3 banner sw.WriteLine("User " + user.Text); //Send username sw.Flush(); response = sr.ReadLine(); if (response.Substring(0, 3) == "-ER") { status.Text = "Unable to log into server"; return; } sw.WriteLine("Pass " + passwd.Text); //Send password sw.Flush(); try { response = sr.ReadLine(); } catch (IOException) { status.Text = "Unable to log into server"; return; } if (response.Substring(0, 4) == "-ERR") { status.Text = "Unable to log into server"; return; } sw.WriteLine("stat"); //Send stat command to get number of messages sw.Flush(); response = sr.ReadLine(); string[] nummess = response.Split(' '); totmessages = Convert.ToInt16(nummess[1]); if (totmessages > 0) { status.Text = "you have " + totmessages + " messages"; } else { status.Text = "You have no messages"; } for (int i = 1; i <= totmessages; i++) { sw.WriteLine("top " + i + " 0"); //read header of each message sw.Flush(); response = sr.ReadLine(); while (true) { response = sr.ReadLine(); if (response == ".") break; if (response.Length > 4) { if (response.Substring(0, 5) == "From:") from = response; if (response.Substring(0, 8) == "Subject:") subject = response; } } results.Items.Add(i + " " + from + " " + subject); } } void getmessages_DoubleClick(object obj, EventArgs ea) { string text = (string)results.SelectedItem; string[] textarray = text.Split(' '); ShowMessage sm = new ShowMessage(ns, textarray[0]); sm.ShowDialog(); } }
class ShowMessage : Form { public ShowMessage(NetworkStream ns, string messnumber) { StreamReader sr = new StreamReader(ns); StreamWriter sw = new StreamWriter(ns); string response; Text = "Message " + messnumber; Size = new Size(400, 380); ShowInTaskbar = false; TextBox display = new TextBox(); display.Parent = this; display.Multiline = true; display.Dock = DockStyle.Fill; display.ScrollBars = ScrollBars.Both; sw.WriteLine("retr " + messnumber); //Retrieve entire message sw.Flush(); response = sr.ReadLine(); while (true) { response = sr.ReadLine(); if (response == ".") break; display.Text += response + "\r\n"; } } } }
|
Check receive mail:
Double click để xem thông tin của mail: _________________ A person who never made a mistake never tried anything new! http://www.echip.com.vn http://www.pcworld.com.vn http://www.khoahocphothong.com.vn |
|