如何从c#发送邮件
我有代码,
System.Web.Mail.MailMessage oMailMessage = new MailMessage();
oMailMessage.From = strFromEmaild;
oMailMessage.To = strToEmailId;
oMailMessage.Subject = strSubject;
oMailMessage.Body = strBody;
SmtpMail.SmtpServer = "localhost";
SmtpMail.Send(oMailMessage);
(所有变量都有值)
我已经安装了SMTP虚拟服务。为什么无法发送电子邮件。为什么它不工作?
编辑
public bool SendMail(string strToEmailId, string strFromEmaild, string strSubject, string strBody)
{
try
{
System.Web.Mail.MailMessage oMailMessage = new MailMessage();
oMailMessage.From = strFromEmaild;
oMailMessage.To = strToEmailId;
oMailMessage.Subject = strSubject;
oMailMessage.Body = strBody;
SmtpMail.SmtpServer = "SERVERNAME";
SmtpMail.Send(oMailMessage);
return true;
}
catch (Exception ex)
{
return false;
}
}
我有这个代码。它执行良好并且返回 true,但我在收件箱中没有收到任何电子邮件。
还有什么可能是错误的?
在 C:\Inetpub\mailroot\Badmail 的 BadMail Dir 中获取一些邮件也在队列目录中获取一些邮件在这里...这意味着什么..?
我发现邮件只能发送到gmail帐户...为什么呢?
I have code,
System.Web.Mail.MailMessage oMailMessage = new MailMessage();
oMailMessage.From = strFromEmaild;
oMailMessage.To = strToEmailId;
oMailMessage.Subject = strSubject;
oMailMessage.Body = strBody;
SmtpMail.SmtpServer = "localhost";
SmtpMail.Send(oMailMessage);
(all variables have values)
I have installed SMTP virtual services. why it is unable to send emails. why it is not working ??
EDIT
public bool SendMail(string strToEmailId, string strFromEmaild, string strSubject, string strBody)
{
try
{
System.Web.Mail.MailMessage oMailMessage = new MailMessage();
oMailMessage.From = strFromEmaild;
oMailMessage.To = strToEmailId;
oMailMessage.Subject = strSubject;
oMailMessage.Body = strBody;
SmtpMail.SmtpServer = "SERVERNAME";
SmtpMail.Send(oMailMessage);
return true;
}
catch (Exception ex)
{
return false;
}
}
I have this code. It is executing fine and is returning true, but I'm not getting any email in the inbox.
What else could be wrong?
Getting some mails in BadMail Dir at C:\Inetpub\mailroot\Badmail also in Queue Directory getting some mails here ... what does that means..??
I found that mail only can sent to gmail accounts... why it is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
正如其他人所提到的,您的代码很好,很可能是您的 SMTP 配置中的某些内容,或者您发送测试电子邮件的电子邮件客户端将它们标记为垃圾邮件。如果是垃圾邮件,那么很容易找出来。
如果与电子邮件有关,您可以转到您的邮件根文件夹,其中将包含一些文件夹,其中包含电子邮件文件和说明。查看死信文件夹或队列文件夹中是否有任何内容,然后在记事本中打开它们,并查看给出的错误以及未发送的原因。
As mentioned by others, your code is fine and is most likely something in your SMTP configuration or maybe your email client your sending your test emails to is marking them as spam. If it's spam, well that's easy enoughto figure out.
If it's something with the email, you can go to your mailroot folder and their will be some folders there with the email files along with a description. See if there's anything in the BadMail folder or the queue folder and open them up in notepad and view what error is given for why they weren't sent.
确定错误是什么:
从这里,请使用该异常详细信息编辑您的问题。
Determine what the error is:
From here, please edit your question with that exception details.
这很难说,但一种可能是您没有在 SMTP 虚拟服务器上启用匿名访问。转到虚拟服务器属性对话框,选择“访问”选项卡,单击“访问控制”按钮,并确保启用“匿名访问”。
Its hard to tell, but one possibility is that you haven't enabled anonymous access on the SMTP virtual server. Go to the the virtual server properties dialog, select the Access tab, click the Access Control button, and make sure that Anonymous Access is enabled.
您的程序似乎没有任何功能错误。这可能是您的程序和邮件服务器之间的配置问题。我会尝试以下方法来诊断问题。
There doesn't appear to be anything functionally wrong with your program. It's likely a configuration issue between your program and the mail server. I would try the following to diagnose the problem.
您好,您可以按照以下代码操作:
Hello you can follow the following code:
您尝试过 127.0.0.1 而不是 Localhost 吗?
另外,您是否已测试 SMTP 服务是否正常工作,请查看此链接了解详细信息。
have you tried 127.0.0.1 instead of Localhost?
Also have you tested that the SMTP service is working, check out this link for details.
在虚拟 smtp 服务器中添加中继限制和连接控制,以便不允许任何外部连接
In the virtual smtp server Add relay restrictions and connection control so that none of the outside connections are allowed