如何从我的 C# 应用程序发送电子邮件?
这是我写的代码:
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);
它不起作用。 我在“client.Send(mail)”行遇到异常,显示“邮箱不可用。服务器响应为 (MYLOCALCOMPUTERNAME) [MY LOCAL IP]:3045 当前不允许中继。”
connectAccess.Access 确实返回“Connect”(我不确定这是否有必要......我将其添加到开始故障排除过程中。)
这是否意味着我的本地计算机必须以某种方式进行配置? 当我将我的应用程序部署到其他人的机器上时会怎么样? 那里需要本地配置吗? 我只是想从我的应用程序创建一个“发送反馈”类型的链接。
(注意:在我的真实应用程序中,我在“收件人”和“发件人”中使用我的真实电子邮件地址,并且我的 smtp 实际上是我在托管我的 url/网站的地方的 smtp 地址)
谢谢!
-阿迪娜
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."
connectAccess.Access does return "Connect" (I'm not sure if this was necessary... I added it in to start the troubleshooting process.)
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)
thanks!
-Adeena
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
@迈克尔:感谢您的链接。 这非常有帮助。
我认为我已经解决了我的问题。 创建“客户端”对象后,我确实需要添加登录凭据。 我添加了以下行:(
抱歉 - 我有这样的习惯,在我在网上搜索答案并通过我的手册 2 小时后,我终于崩溃并发布问题,然后 5 分钟后弄清楚。:)我认为写下问题的行为比其他任何事情都对我有帮助)
所以它正在起作用......尽管我不会声称我了解它如何以及为什么起作用的所有内容,所以我确实预计会遇到一些问题,因为我给出了我的程序给别人用。 即,使用具有互联网连接的程序的每个人都能够打开到我的服务器的此 smtp 连接吗? 我不知道答案……我必须等待、观察并了解更多。
谢谢! :)
-阿迪娜
@ Michael: thanks for the link. It's very helpful.
I think I figured out my problem. I did need to add the login credentials after I created my "client" object. I added the following line:
(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)
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.
Thanks! :)
-Adeena
目标地址与您的 smtp 服务器位于同一主机上吗? 如果不是,这将解释中继错误。
您使用的 SMTP 服务器必须是邮件消息的最终目的地或邮件交换中的第一跃点。 例如,如果您从 gmail 地址向 yahoo 地址发送邮件,则第一个查看该邮件的邮件服务器必须是您的 gmail 服务器或 yahoo 服务器。 中间的服务器将拒绝该消息,因为它们已禁用中继(以减少垃圾邮件等)。
如果它们是同一主机,您可以通过其他方式直接向其发送邮件吗?
通过 telnet 尝试此测试,看看您的 smtp 服务器是否运行正常: http://www.messagingtalk .org/content/470.html
Is the destination address on the same host as your smtp server? If not, this would explain a relaying error.
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.).
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
检查你的防火墙。 2525帖子还开吗?
Check your firewall. Is 2525 post open?