邮件发送器C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;
 
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Mail To");
        MailAddress to = new MailAddress(Console.ReadLine());
 
        Console.WriteLine("Mail From");
        MailAddress from = new MailAddress(Console.ReadLine());
 
        MailMessage mail = new MailMessage(from, to);
 
        Console.WriteLine("Subject");
        mail.Subject = Console.ReadLine();
 
        Console.WriteLine("Your Message");
        mail.Body = Console.ReadLine();
 
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
 
        smtp.Credentials = new NetworkCredential(
            "[email protected]", "password");
        smtp.EnableSsl = true;
        Console.WriteLine("Sending email...");
        smtp.Send(mail);
    }
}

 

class SendMail 
    { 
        private string MailTo; 
        private string MailFrom;       
        private string MailFromPass; 
        private string Message; 
 
        public SendMail(string _MailFrom, string _MailFromPass, string _MailTo, string _Message) 
        { 
            MailTo = _MailTo; 
            MailFrom = _MailFrom; 
            MailFromPass = _MailFromPass; 
            Message = _Message; 
        } 
 
        public void sendMail() 
        { 
            try 
            { 
                MailMessage mail = new MailMessage(); 
                mail.From = new MailAddress(MailFrom); 
                mail.To.Add(new MailAddress(MailTo)); 
                mail.Body = Message; 
 
                SmtpClient Smtp_Client = new SmtpClient(); 
                 
                 
 
                if (MailFrom.Contains("gmail")) 
                { 
                    Smtp_Client = new SmtpClient("smtp.gmail.com", 587); 
                    Smtp_Client.EnableSsl = true; 
                } 
                else if (MailFrom.Contains("live")) 
                { 
                    Smtp_Client = new SmtpClient("smtp.live.com", 587); 
                    Smtp_Client.EnableSsl = true; 
                } 
                else if (MailFrom.Contains("yahoo")) 
                { 
                    Smtp_Client = new SmtpClient("smtp.mail.yahoo.com", 587); 
                    //Yahoo don't support SSL 
                    Smtp_Client.EnableSsl = false; 
                } 
 
                Smtp_Client.Credentials = new NetworkCredential(MailFrom, MailFromPass); 
                 
 
                Smtp_Client.SendCompleted += new SendCompletedEventHandler(smtp_SendCompleted); 
                Smtp_Client.SendAsync(mail, null); 
            } 
            catch (Exception ex) 
            { 
                MessageBox.Show(ex.ToString()); 
            } 
        } 
 
        private void smtp_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) 
        { 
            if (e.Error != null) 
                MessageBox.Show(e.Error.Message); 
            else if (e.Cancelled) 
                MessageBox.Show("Cancelled sending !"); 
            else 
                MessageBox.Show("Mail successfully sent!"); 
        }


你可能感兴趣的:(C#)