无法从C#发送邮件 [英] Can't send mail from C#

查看:88
本文介绍了无法从C#发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从c#发送电子邮件时,我收到拒绝中继错误。

使用Thunderbird的相同凭据一切正常。

我使用Postfix和Dovecote运行我自己的邮件服务器。



以下是我的代码:

When I send an email from c# I get a "relay access denied" error.
Using the same credentials from Thunderbird everything works fine.
I run my own mail server with Postfix and Dovecote.

Below is my code:

System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
    client.Host = "mail.mydomainname.nl";
    client.Port = 2525;
    client.Credentials = new System.Net.NetworkCredential("username@mydomainname.nl", "correctpassword");
    client.UseDefaultCredentials = false;

string Van = "username@mydomainname.nl";


System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage("username@mydomainname.nl", "somebody@hisdomainname.nl", "Subject of this mail", "Contents of this mail");

try
{
    client.Send(msg);
}
catch(Exception e)
{
    System.Windows.Forms.MessageBox.Show("Error sending email: \r\n" + e.Message );
    return false;
}

return true;





提前致谢,



Rob



编辑2012-12-04:

我目前使用/etc/postfix/main.cf中的myNetworks条目添加IP我正在发送。由于几个原因,这不是理想的,所以我仍然在寻找更好的解决方案。



Rob



Thanks in advance,

Rob

Edit 2012-12-04:
I currently use the myNetworks entry in /etc/postfix/main.cf to add the IP from which I''m sending. This ain''t ideal for several reasons so I''m still looking for a better solution.

Rob

推荐答案

拒绝中继访问通常表示您正在使用的服务器发送电子邮件不喜欢电子邮件的发件人字段中的域名。您是否尝试从与您使用的SMTP服务器不同的域发送电子邮件作为用户?例如,使用Google SMTP服务器发送带有发件人地址的电子邮件,例如 someuser@yahoo.com
"Relay access denied" usually means the server you''re using to send the email doesn''t like the domain in the From field of the email. Are you trying to send an email as a user from a different domain than the SMTP server you''re using to send it?? For example, using a Google SMTP server to send an email with a From address like someuser@yahoo.com.


嗨亲爱的,



查看此解决方案。



Hi dear,

Check this solution.

private void MailSend()
       {
           try
           {
               MailMessage mail = new MailMessage();
               SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
               SmtpServer.Host = "smtp.gmail.com";

               mail.From = new MailAddress("FromMailName@gamil.com");
               mail.To.Add("ToMailName@gmail.com");
               mail.Subject = "Test Mail";
               mail.Body = "This is for testing SMTP mail from GMAIL";

               SmtpServer.Port = 587;

               SmtpServer.Credentials = new System.Net.NetworkCredential("ToMailName@gmail.com", "123456"); // User Mail Name And Mail Password
               SmtpServer.EnableSsl = true;

               SmtpServer.Send(mail);
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.ToString());
           }
       }


这篇关于无法从C#发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆