【发布时间】:2011-12-14 15:10:39
【问题描述】:
我正在构建一个应用程序,用户在其中创建电子邮件,电子邮件从他的邮件服务器发送。我不希望收到任何电子邮件,只是发送它们。我需要访问他的邮件服务器来执行此操作,我想知道 a) 那里有什么样的电子邮件服务器(Exchange、SMTP、POP3 ......)以及用户需要向我提供什么样的信息(即什么我需要数据库中的字段)。
如果您知道要避免的任何陷阱,请告诉我。
感谢您的建议。
【问题讨论】:
我正在构建一个应用程序,用户在其中创建电子邮件,电子邮件从他的邮件服务器发送。我不希望收到任何电子邮件,只是发送它们。我需要访问他的邮件服务器来执行此操作,我想知道 a) 那里有什么样的电子邮件服务器(Exchange、SMTP、POP3 ......)以及用户需要向我提供什么样的信息(即什么我需要数据库中的字段)。
如果您知道要避免的任何陷阱,请告诉我。
感谢您的建议。
【问题讨论】:
SMTP 和 POP3 是协议,而不是电子邮件服务器。
如果我理解正确,您需要根据构建电子邮件的客户端连接到不同的电子邮件服务器,以通过他/她的邮件服务器发送电子邮件(?)
如果是这样,您需要为您的每个客户找出他们的邮件服务器的 IP 地址以及他们支持的身份验证、加密等类型。建立后,您需要使用客户端提供的特定凭据连接到每个服务器,并通过他们的 SMTP 服务器发送电子邮件。示例:
// Create a message and set up the recipients.
MailMessage message = new MailMessage(
"jane@contoso.com",
"ben@contoso.com",
"Quarterly data report.",
"Hello, test email!.");
//Send the message.
SmtpClient client = new SmtpClient(server);
// Add credentials if the SMTP server requires them.
// YOU NEED TO CHANGE THIS PART DEPENDING ON THE SPECIFICS OF THE
//SMTP SERVER THAT YOU WILL BE USING
client.Credentials = CredentialCache.DefaultNetworkCredentials;
try {
client.Send(message);
}
catch (Exception ex) {
Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}",
ex.ToString() );
}
上面的例子几乎是逐字记录的,from here.
【讨论】:
发送电子邮件很容易,如何存储和管理这取决于您:
web.config:
<system.net>
<mailSettings>
<smtp>
<network
host="relayServerHostname"
port="portNumber"
userName="username"
password="password" />
</smtp>
</mailSettings>
</system.net>
代码:
MailMessage mail = new MailMessage(from, to, subject, message);
mail.IsBodyHtml = true;
SmtpClient client = new SmtpClient(); //used config settings
client.Send(mail);
【讨论】: