如何从c#发送邮件

发布于 2024-08-18 15:20:25 字数 1205 浏览 7 评论 0原文

我有代码,

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

街角卖回忆 2024-08-25 15:20:25

正如其他人所提到的,您的代码很好,很可能是您的 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.

自由如风 2024-08-25 15:20:25

确定错误是什么:

try
{
 SmtpMail.Send(oMailMessage);
}
catch (Exception ex)
{
//breakpoint here to determine what the error is:
Console.WriteLine(ex.Message);
}

从这里,请使用该异常详细信息编辑您的问题。

Determine what the error is:

try
{
 SmtpMail.Send(oMailMessage);
}
catch (Exception ex)
{
//breakpoint here to determine what the error is:
Console.WriteLine(ex.Message);
}

From here, please edit your question with that exception details.

吹泡泡o 2024-08-25 15:20:25

这很难说,但一种可能是您没有在 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.

习惯成性 2024-08-25 15:20:25

您的程序似乎没有任何功能错误。这可能是您的程序和邮件服务器之间的配置问题。我会尝试以下方法来诊断问题。

  1. 将代码包装在 try/catch 块中,看看异常消息是否包含有用的数据
  2. 使用 127.0.0.1 而不是 localhost 只是为了排除任何疯狂的情况
  3. 确保您的 SMTP 服务器在标准端口上运行(我相信是 25)

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.

  1. Wrap the code in a try/catch block and see if the exception message contains useful data
  2. Use 127.0.0.1 instead of localhost just to rule out anything crazy
  3. Ensure your SMTP server is running on the standard port (25 I believe)
我纯我任性 2024-08-25 15:20:25

您好,您可以按照以下代码操作:

try
        {
            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            client.EnableSsl = true;
            client.Timeout = 100000;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential("your gmail id", "password");
            MailMessage msg = new MailMessage();
            msg.To.Add(textBoxTo.Text);
            msg.From = new MailAddress("your gmail id");
            msg.Subject = textBoxSubject.Text;
            msg.Body = textBoxMsg.Text;
            Attachment data = new Attachment(textBoxAttachment.Text);
            msg.Attachments.Add(data);
            client.Send(msg);
            MessageBox.Show("Successfully Sent Message.");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
       }

Hello you can follow the following code:

try
        {
            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            client.EnableSsl = true;
            client.Timeout = 100000;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential("your gmail id", "password");
            MailMessage msg = new MailMessage();
            msg.To.Add(textBoxTo.Text);
            msg.From = new MailAddress("your gmail id");
            msg.Subject = textBoxSubject.Text;
            msg.Body = textBoxMsg.Text;
            Attachment data = new Attachment(textBoxAttachment.Text);
            msg.Attachments.Add(data);
            client.Send(msg);
            MessageBox.Show("Successfully Sent Message.");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
       }
彼岸花似海 2024-08-25 15:20:25

您尝试过 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.

孤君无依 2024-08-25 15:20:25

在虚拟 smtp 服务器中添加中继限制和连接控制,以便不允许任何外部连接

在此处输入图像描述

In the virtual smtp server Add relay restrictions and connection control so that none of the outside connections are allowed

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文