谷歌邮件类

本文关键字:谷歌 | 更新日期: 2023-09-27 18:34:52

我有一个如下所示的谷歌邮件程序,我如何将其应用于其他邮件系统? 有没有更好的方法?另外,我如何更改邮件来源(发件人(的名称。所有的帮助将不胜感激,提前感谢您

 MailMessage message = new MailMessage();
        message.From = new MailAddress(MailAddresds);
        message.Subject = messagesubject;
        message.Body = messagebody;

        message.To.Add(messageto);
        SmtpClient client = new SmtpClient();
        client.Credentials = new NetworkCredential(userName, password);
        client.Host = "smtp.gmail.com";
        client.Port = 587;
        client.EnableSsl = true;
        client.Send(message);

谷歌邮件类

举个例子:

public class MailMessage
{
   public string From{get;set;}
   public string To{get;set;}
   public string Body{get;set;}
   public string Subject{get;set;}
   ....
   //other common properties you may need
}

//interface 
public interface IMailService
{
    Send(MailMessage m);
}

具体实施:

public class GoogleMail : IMailService
{
    public Send(Message msg)
    { 
       //google mail specific code
    }
}

public class YahooMail : IMailService
{
    public Send(Message msg)
    { 
       //yahoo mail specific code
    }
}
.... Hotmail,...

在代码中的某处创建受支持的邮件服务的集合

var mailservices = new List<IMailService>();
mailservices.Add(new GoogleMail ());
mailservice.Add(new YahooMail ());

之后,在程序运行期间,选择适当的服务以继续用户请求。

一个简单的例子:

using System.Web.Mail;
MailMessage objMessage = new MailMessage();
objMessage.From = "from";
objMessage.To = "to";
objMessage.Subject = "subject";
objMessage.BodyFormat = MailFormat.Text;
objMessage.Body = "body";
SmtpMail.SmtpServer = "SmtpServer";
SmtpMail.Send(objMessage);