如何从我的 C# 应用程序发送电子邮件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/366629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I send an email message from my C# application?
提问by adeena
This is the code I wrote:
这是我写的代码:
MailMessage mail = new MailMessage("[email protected]", "[email protected]");
mail.Subject = "This is a test!!";
mail.Body = "testing...";
SmtpPermission connectAccess = new SmtpPermission(SmtpAccess.Connect);
System.Console.WriteLine("Access? " + connectAccess.Access);
SmtpClient client = new SmtpClient("mail.myurl.com", 2525);
client.Send(mail);
It's not working. I get an exception at the line "client.Send(mail)" that says "Mailbox unavailable. The server response was (MYLOCALCOMPUTERNAME) [MY LOCAL IP]:3045 is currently not permitted to relay through."
它不起作用。我在“client.Send(mail)”行出现异常,显示“邮箱不可用。服务器响应为 (MYLOCALCOMPUTERNAME) [MY LOCAL IP]:3045 目前不允许中继通过。”
connectAccess.Access does return "Connect" (I'm not sure if this was necessary... I added it in to start the troubleshooting process.)
connectAccess.Access 确实返回“Connect”(我不确定这是否有必要......我添加了它以开始故障排除过程。)
Does this mean that my local machine has to be configured in some way? What about when I deploy my app to other peoples machines? Will there need to be local configuration there? I'm just looking to create a "Send Feedback" type of link from my application.
这是否意味着必须以某种方式配置我的本地机器?当我将我的应用程序部署到其他人的机器时呢?那里需要本地配置吗?我只是想从我的应用程序中创建一个“发送反馈”类型的链接。
(Note: in my real application I am using my real email addresses in both the "to" and "from" and my smtp is really my smtp address at the place that hosts my url/website)
(注意:在我的实际应用程序中,我在“收件人”和“发件人”中都使用了我的真实电子邮件地址,而我的 smtp 实际上是托管我的 url/网站的地方的 smtp 地址)
thanks!
谢谢!
-Adeena
-阿迪娜
回答by Michael Haren
Is the destination address on the same host as your smtp server? If not, this would explain a relaying error.
目标地址是否与您的 smtp 服务器在同一台主机上?如果不是,这将解释中继错误。
The SMTP server you use needs to be either the final destination of the mail message or the first hop in the mail exchange. For example, if you're sending mail to a yahoo address from a gmail address, the first mail server to see the message must be your gmail server, or the yahoo server. Servers in between will reject the message because they have relaying disabled (to cut down on spam, etc.).
您使用的 SMTP 服务器需要是邮件消息的最终目的地或邮件交换中的第一个跃点。例如,如果您要从 gmail 地址向 yahoo 地址发送邮件,那么第一个看到该邮件的邮件服务器必须是您的 gmail 服务器或 yahoo 服务器。中间的服务器将拒绝该消息,因为它们已禁用中继(以减少垃圾邮件等)。
If they are the same host, are you able to send mail to it directly any other way?
如果它们是同一台主机,您是否可以通过其他方式直接向其发送邮件?
Try this test via telnet to see if your smtp server is behaving properly: http://www.messagingtalk.org/content/470.html
通过 telnet 尝试此测试以查看您的 smtp 服务器是否运行正常:http: //www.messagingtalk.org/content/470.html
回答by adeena
@ Michael: thanks for the link. It's very helpful.
@迈克尔:谢谢你的链接。这是非常有帮助的。
I thinkI figured out my problem. I did need to add the login credentials after I created my "client" object. I added the following line:
我想我找到了我的问题。在创建“客户端”对象后,我确实需要添加登录凭据。我添加了以下行:
client.Credentials = new System.Net.NetworkCredential("myloginat+myurl.com", "mypassword");
(sorry - I have this habit that after I search for an answer on the web and through my manuals for 2 hrs, I finally break down and post the question and then 5 minutes later figure it out. :) I think the act of writing down the question helps me more than anything else)
(对不起 - 我有一个习惯,在我在网上和我的手册上搜索了 2 小时的答案后,我终于分解并发布了问题,然后 5 分钟后弄清楚了。:) 我认为写作的行为这个问题对我的帮助比什么都重要)
So it's working... although I won't claim I understand everything about how and why it's working so I do expect to run in to some problems as I give my program to others to use. i.e., will everyone using the program that has an internet connection be able to open this smtp connection to my server? I don't know the answer to that... I'll have to wait, see, and learn some more.
所以它正在工作......虽然我不会声称我了解它如何以及为什么工作的一切,所以当我将我的程序提供给其他人使用时,我确实希望遇到一些问题。即,使用具有 Internet 连接的程序的每个人都能够打开此 smtp 连接到我的服务器吗?我不知道答案……我将不得不等待、观察并了解更多信息。
Thanks! :)
谢谢!:)
-Adeena
-阿迪娜
回答by iburlakov
Check your firewall. Is 2525 post open?
检查您的防火墙。2525的帖子开放了吗?
回答by Shaik Raffi
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;
namespace SendMail
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
SmtpClient client = new SmtpClient("smtp.gmail.com", 25);
MailMessage msg = new MailMessage();
NetworkCredential cred = new NetworkCredential("[email protected]", "password");
msg.From = new MailAddress("[email protected]");
msg.To.Add("[email protected]");
msg.Subject = "A subject";
msg.Body = "Hello,Raffi";
client.Credentials = cred;
client.EnableSsl = true;
label1.Text = "Mail Sended Succesfully";
client.Send(msg);
}
catch
{
label1.Text = "Error";
}
}
}
}