Google Mailer Class

时间:2012-05-22 12:30:53

标签: c#

我有一个谷歌邮件如下图所示,我如何适应其他邮件系统?有没有更好的方法呢?我如何更改邮件来自的位置(发件人)。 非常感谢所有帮助,并提前感谢您

 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);

2 个答案:

答案 0 :(得分:1)

只是示例:

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 ());

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

答案 1 :(得分:1)

简单的例子:

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);