邮件发送器C#

本文提供了一个使用C#发送邮件的示例程序,通过控制台读取发件人、收件人信息,并利用SMTP客户端发送邮件。支持Gmail、Live和Yahoo等邮箱服务。
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(
            "username@domain.com", "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!"); 
        }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值