我在Server 2008上使用IIS 7.
我刚尝试从较旧的平台迁移我的应用程序 - 一切正常,但电子邮件功能除外。
这是我的配置:
< mailSettings >
< smtp from="abc@xyz.com" deliveryMethod="Network" >
< network host="mail.xyz.com" port="25" userName="abc@xyz.com" password="123" /> < / smtp >
< /mailSettings >
每当我需要发送电子邮件时,我都会使用:
SmtpClient smtp = new SmtpClient();
smtp.Send(email);
有趣的是我绝对没有错误,但是电子邮件永远不会被发送。
出站防火墙规则集允许SMTP流量。
知道我做错了吗?
答案 0 :(得分:2)
如果您没有收到任何例外,那么我将启用SNM的日志记录。这至少会告诉您邮件是否由SNM发送,如果是,那么您可以查看其他问题。
要启用日志记录,请将以下内容添加到.config文件中:
<configuration>
<sources>
<source name="System.Net" >
<listeners>
<add name="MyTraceFile"/>
</listeners>
</source>
<source name="System.Net.Sockets">
<listeners>
<add name="MyTraceFile"/>
</listeners>
</source>
</sources>
<sharedListeners>
<add
name="MyTraceFile"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="System.Net.trace.log" />
</sharedListeners>
<switches>
<add name="System.Net" value="Verbose" />
<add name="System.Net.Sockets" value="Verbose" />
</switches>
以下是一个示例日志文件:
System.Net Verbose: 0 : [0992] SmtpClient::.ctor(host=127.0.0.1)
System.Net Information: 0 : [0992] Associating SmtpClient#47606018 with SmtpTransport#5689696
System.Net Verbose: 0 : [0992] Exiting SmtpClient::.ctor() -> SmtpClient#47606018
System.Net Verbose: 0 : [0992] SmtpClient#47606018::Send(MailMessage#5138334)
System.Net Information: 0 : [0992] SmtpClient#47606018::Send(DeliveryMethod=Network)
System.Net Information: 0 : [0992] Associating SmtpClient#47606018 with MailMessage#5138334
System.Net Information: 0 : [0992] Associating SmtpTransport#5689696 with SmtpConnection#31950948
System.Net Information: 0 : [0992] Associating SmtpConnection#31950948 with ServicePoint#34920472
System.Net.Sockets Verbose: 0 : [0992] Socket#22453229::Socket(InterNetwork#2)
System.Net.Sockets Verbose: 0 : [0992] Exiting Socket#22453229::Socket()
System.Net.Sockets Verbose: 0 : [0992] Socket#22453229::Connect(1:25#16777318)
System.Net.Sockets Verbose: 0 : [0992] Exiting Socket#22453229::Connect()
System.Net Information: 0 : [0992] Associating SmtpConnection#31950948 with SmtpPooledStream#48167163
System.Net.Sockets Verbose: 0 : [0992] Socket#22453229::Receive()
System.Net.Sockets Verbose: 0 : [0992] Data from Socket#22453229::Receive
System.Net.Sockets Verbose: 0 : [0992] 00000000 : 32 32 30 20 77 32 6B 20-4D 69 63 72 6F 73 6F 66 : 220 w2k Microsof
System.Net.Sockets Verbose: 0 : [0992] 00000010 : 74 20 45 53 4D 54 50 20-4D 41 49 4C 20 53 65 72 : t ESMTP MAIL Ser
System.Net.Sockets Verbose: 0 : [0992] 00000020 : 76 69 63 65 2C 20 56 65-72 73 69 6F 6E 3A 20 35 : vice, Version: 5
System.Net.Sockets Verbose: 0 : [0992] 00000030 : 2E 30 2E 32 31 39 35 2E-36 37 31 33 20 72 65 61 : .0.2195.6713 rea
System.Net.Sockets Verbose: 0 : [0992] 00000040 : 64 79 20 61 74 20 20 53-61 74 2C 20 33 31 20 44 : dy at Sat, 31 D
System.Net.Sockets Verbose: 0 : [0992] 00000050 : 65 63 20 32 30 30 35 20-32 32 3A 31 33 3A 31 34 : ec 2005 22:13:14
System.Net.Sockets Verbose: 0 : [0992] 00000060 : 20 2D 30 36 30 30 20 0D-0A : -0600 ..
System.Net.Sockets Verbose: 0 : [0992] Exiting Socket#22453229::Receive() -> 105#105
System.Net.Sockets Verbose: 0 : [0992] Socket#22453229::Send()
System.Net.Sockets Verbose: 0 : [0992] Data from Socket#22453229::Send
System.Net.Sockets Verbose: 0 : [0992] 00000000 : 45 48 4C 4F 20 77 32 6B-0D 0A : EHLO w2k..
System.Net.Sockets Verbose: 0 : [0992] Exiting Socket#22453229::Send() -> 10#10
System.Net.Sockets Verbose: 0 : [0992] Socket#22453229::Receive()
System.Net.Sockets Verbose: 0 : [0992] Data from Socket#22453229::Receive
System.Net.Sockets Verbose: 0 : [0992] 00000000 : 32 35 30 2D 77 32 6B 20-48 65 6C 6C 6F 20 5B 31 : 250-w2k Hello [1
System.Net.Sockets Verbose: 0 : [0992] 00000010 : 32 37 2E 30 2E 30 2E 31-5D 0D 0A 32 35 30 2D 41 : 27.0.0.1]..250-A
System.Net.Sockets Verbose: 0 : [0992] 00000020 : 55 54 48 20 47 53 53 41-50 49 20 4E 54 4C 4D 20 : UTH GSSAPI NTLM
System.Net.Sockets Verbose: 0 : [0992] 00000030 : 4C 4F 47 49 4E 0D 0A 32-35 30 2D 41 55 54 48 3D : LOGIN..250-AUTH=
System.Net.Sockets Verbose: 0 : [0992] 00000040 : 4C 4F 47 49 4E 0D 0A 32-35 30 2D 54 55 52 4E 0D : LOGIN..250-TURN.
System.Net.Sockets Verbose: 0 : [0992] 00000050 : 0A 32 35 30 2D 41 54 52-4E 0D 0A 32 35 30 2D 53 : .250-ATRN..250-S
System.Net.Sockets Verbose: 0 : [0992] 00000060 : 49 5A 45 20 32 30 39 37-31 35 32 0D 0A 32 35 30 : IZE 2097152..250
System.Net.Sockets Verbose: 0 : [0992] 00000070 : 2D 45 54 52 4E 0D 0A 32-35 30 2D 50 49 50 45 4C : -ETRN..250-PIPEL
System.Net.Sockets Verbose: 0 : [0992] 00000080 : 49 4E 49 4E 47 0D 0A 32-35 30 2D 44 53 4E 0D 0A : INING..250-DSN..
System.Net.Sockets Verbose: 0 : [0992] 00000090 : 32 35 30 2D 45 4E 48 41-4E 43 45 44 53 54 41 54 : 250-ENHANCEDSTAT
System.Net.Sockets Verbose: 0 : [0992] 000000A0 : 55 53 43 4F 44 45 53 0D-0A 32 35 30 2D 38 62 69 : USCODES..250-8bi
System.Net.Sockets Verbose: 0 : [0992] 000000B0 : 74 6D 69 6D 65 0D 0A 32-35 30 2D 42 49 4E 41 52 : tmime..250-BINAR
System.Net.Sockets Verbose: 0 : [0992] 000000C0 : 59 4D 49 4D 45 0D 0A 32-35 30 2D 43 48 55 4E 4B : YMIME..250-CHUNK
System.Net.Sockets Verbose: 0 : [0992] 000000D0 : 49 4E 47 0D 0A 32 35 30-2D 56 52 46 59 0D 0A 32 : ING..250-VRFY..2
System.Net.Sockets Verbose: 0 : [0992] 000000E0 : 35 30 20 4F 4B 0D 0A : 50 OK..
System.Net.Sockets Verbose: 0 : [0992] Exiting Socket#22453229::Receive() -> 231#231
System.Net.Sockets Verbose: 0 : [0992] Socket#22453229::Send()
System.Net.Sockets Verbose: 0 : [0992] Data from Socket#22453229::Send
System.Net.Sockets Verbose: 0 : [0992] 00000000 : 4D 41 49 4C 20 46 52 4F-4D 3A 3C 6D 65 40 6D 79 : MAIL FROM:..
System.Net.Sockets Verbose: 0 : [0992] Exiting Socket#22453229::Send() -> 30#30
System.Net.Sockets Verbose: 0 : [0992] Socket#22453229::Receive()
System.Net.Sockets Verbose: 0 : [0992] Data from Socket#22453229::Receive
System.Net.Sockets Verbose: 0 : [0992] 00000000 : 32 35 30 20 32 2E 31 2E-30 20 6D 65 40 6D 79 63 : 250 2.1.0 me@myc
System.Net.Sockets Verbose: 0 : [0992] 00000010 : 6F 6D 70 61 6E 79 2E 63-6F 6D 2E 2E 2E 2E 53 65 : ompany.com....Se
System.Net.Sockets Verbose: 0 : [0992] 00000020 : 6E 64 65 72 20 4F 4B 0D-0A : nder OK..
System.Net.Sockets Verbose: 0 : [0992] Exiting Socket#22453229::Receive() -> 41#41
System.Net.Sockets Verbose: 0 : [0992] Socket#22453229::Send()
System.Net.Sockets Verbose: 0 : [0992] Data from Socket#22453229::Send
System.Net.Sockets Verbose: 0 : [0992] 00000000 : 52 43 50 54 20 54 4F 3A-3C 68 69 6D 40 68 69 73 : RCPT TO:..
System.Net.Sockets Verbose: 0 : [0992] Exiting Socket#22453229::Send() -> 30#30
System.Net.Sockets Verbose: 0 : [0992] Socket#22453229::Receive()
System.Net.Sockets Verbose: 0 : [0992] Data from Socket#22453229::Receive
System.Net.Sockets Verbose: 0 : [0992] 00000000 : 35 35 30 20 35 2E 37 2E-31 20 55 6E 61 62 6C 65 : 550 5.7.1 Unable
System.Net.Sockets Verbose: 0 : [0992] 00000010 : 20 74 6F 20 72 65 6C 61-79 20 66 6F 72 20 68 69 : to relay for hi
System.Net.Sockets Verbose: 0 : [0992] 00000020 : 6D 40 68 69 73 63 6F 6D-70 61 6E 79 2E 63 6F 6D : m@hiscompany.com
System.Net.Sockets Verbose: 0 : [0992] 00000030 : 0D 0A : ..
System.Net.Sockets Verbose: 0 : [0992] Exiting Socket#22453229::Receive() -> 50#50
System.Net Error: 0 : [0992] Exception in the SmtpClient#47606018::Send - Mailbox unavailable. The server response was: 5.7.1 Unable to relay for him@hiscompany.com
System.Net Error: 0 : [0992] at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
System.Net.Sockets Verbose: 0 : [0992] Socket#22453229::Dispose()
System.Net Verbose: 0 : [0992] Exiting SmtpClient#47606018::Send()
更多信息可以在我的常见问题解答中找到,在这里: http://systemnetmail.com/faq/4.10.aspx
99%的时间,如果没有抛出异常,则中继服务器接受该电子邮件,并且它现在位于中继服务器队列中。或者,它实际上已经交付,但它被标记为垃圾邮件,因此它位于收件人垃圾邮件文件夹(或目标服务器拒绝它)。
答案 1 :(得分:0)
试试这段代码。这也是通过smtp发送电子邮件。
using System.Net;
bool bSuccess;
//the the SMTP host.
SmtpClient client = new SmtpClient();
//SMTP Server
client.Host = CommonVariables.EMAIL_SMTP_SERVER;
//SMTP Credentials
client.Credentials = new NetworkCredential(CommonVariables.EMAIL_USERNAME, CommonVariables.EMAIL_PASSWORD);
//Creating a new mail.
MailMessage mail = new MailMessage();
//Filling 'From' Tab.
mail.From = new MailAddress(CommonVariables.EMAIL_SENDERS_ADDRESS, CommonVariables.EMAIL_SENDERS_NAME);
mail.To.Add("me@yahoo.com");
//Filling mail body.
string szMailBody = "hhgjhgjhg";
string szMailSubject = "sdsds";
mail.Body = szMailBody;
mail.Subject = szMailSubject;
//Send Email.
try
{
client.Send(mail);
bSuccess = true;
}
catch (Exception Ex)
{
bSuccess = false;
}
// Clean up.
mail.Dispose();
为此你不需要在web.config中进行设置。
答案 2 :(得分:0)
来自forum:
System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient();
mailClient.EnableSsl = true;
也
在web.config中设置defaultCredentials = false;