private void cmdSend_Click(object sender, EventArgs e) { SMTP.SmtpDirect.SmtpServer= "10.108.7.138"; SMTP.SmtpDirect.SmtpServer = txtSmtp.Text; SMTP.SmtpDirect.SmtpPort = Int32.Parse(txtPort.Text); MailMessage message =new MailMessage(); message.Body = txtBody.Text; message.From = txtFrom.Text; message.To = txtTo.Text; message.Subject = txtSubject.Text; if (txtAttachment.Text != "") { MailAttachment attachment = new MailAttachment(txtAttachment.Text); message.Attachments.Add(attachment); } if(SMTP.SmtpDirect.Send(message)) { MessageBox.Show("Sent OK"); } else { MessageBox.Show("Something BAD Happened!"); } txtTo.Text = ""; txtFrom.Text = ""; txtSubject.Text = ""; txtAttachment.Text = ""; txtSmtp.Text = ""; txtPort.Text = ""; txtBody.Text = ""; }
public static bool Send(MailMessage message) { IPHostEntry IPhst = Dns.GetHostEntry(SmtpServer); IPEndPoint endPt = new IPEndPoint(IPhst.AddressList[0], SmtpPort); Socket s = new Socket(endPt.AddressFamily, SocketType.Stream, ProtocolType.Tcp); Console.WriteLine("connecting.........."); s.Connect(endPt); Console.WriteLine("connected........"); string msg = ""; msg = msg + message.From + "\n"; msg = msg + message.To + "\n"; msg = msg + message.Subject + "\n"; int attch_count = 0; if (message.Attachments.Count > 0) attch_count = message.Attachments.Count; msg = msg + attch_count + "\n"; if (attch_count > 0) { foreach (object o in message.Attachments) { MailAttachment a = o as MailAttachment; //int pos = a.Filename.LastIndexOf('\\'); char[] delim = new char[1]; delim[0] = '\\'; string[] nameParts = a.Filename.Split(delim); //nt count = a.Filename.Length - (pos+1); //msg = msg + a.Filename.Substring(pos+1, count) + "\n"; string filename = nameParts[nameParts.Length - 1]; filename = Path.GetFileName(a.Filename); msg = msg + filename + "\n"; } } msg = msg + message.Body; Senddata(s, msg, false); if (Check_Response(s, SMTPResponse.MSGLEN_SUCCESS)) { //MessageBox.Show("length acked"); } if (message.Attachments.Count > 0) { foreach (object o in message.Attachments) { MailAttachment a = o as MailAttachment; byte[] binaryData; if (a != null) { FileInfo f = new FileInfo(a.Filename); FileStream fs = f.OpenRead(); int chunksize = 600000; for (int j = 0; j < fs.Length; j = j + chunksize) { int count = chunksize; if ( (j + count )> fs.Length) count = (int)(fs.Length -(long)j); int bytesRead = 0; binaryData = null; binaryData = new Byte[count]; fs.Seek((long)j, SeekOrigin.Begin); while (bytesRead < count) { int bRead = fs.Read(binaryData, bytesRead, count - bytesRead); bytesRead = bytesRead + bRead; } string base64String = System.Convert.ToBase64String(binaryData, 0, binaryData.Length); SendMsgPart(s, base64String); } SendMsgPart(s, "__done__"); fs.Close(); } } } return true; }
static void Main(string[] args) { string UserEmailAddress; int LineOfInput; string UserPassword; char Option; while (true) { Console.WriteLine("Send E-mails."); Console.WriteLine("Enter Recipient's E-mail address:"); MailAddress to = new MailAddress(Console.ReadLine()); //user inputs the revicers email address Console.WriteLine("Enter Your E-mail Address:"); UserEmailAddress = Console.ReadLine(); MailAddress from = new MailAddress(UserEmailAddress);//senders email address Console.WriteLine("Enter password:"******"Subject"); mail.Subject = Console.ReadLine(); Console.WriteLine("Your message here"); mail.Body = Console.ReadLine(); SmtpClient SMTP = new SmtpClient(); SMTP.Host = "smtp.gmail.com"; //can be changed to more sutable one SMTP.Port = 587; //can be changed to more sutable one SMTP.Credentials = new NetworkCredential(UserEmailAddress, UserPassword); SMTP.EnableSsl = true; Console.WriteLine("Choose option: 'a'(normal), 'b'(spam)"); Option = Console.ReadLine()[0]; if (Option == 'a') { Console.WriteLine("Sending E-mail"); SMTP.Send(mail); UserPassword = null;//clear password. } else if (Option == 'b') { Console.WriteLine("Sending E-mails"); Console.WriteLine("The recipients going to have a bad time."); while (1 < 2) { SMTP.Send(mail);//sends the email. } } }//end of loop }