简体   繁体   English

如何从C#应用程序发送电子邮件?

[英]How do I send an email message from my C# application?

This is the code I wrote: 这是我写的代码:

        MailMessage mail = new MailMessage("test@gmail.com", "me@myurl.com");

        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)[我的本地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确实返回“连接”(我不确定这是否必要......我已将其添加进入以启动故障排除过程。)

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) (注意:在我的实际应用程序中,我使用的是“to”和“from”中的真实电子邮件地址,而我的smtp实际上是我托管我的网址/网站的地方的smtp地址)

thanks! 谢谢!

-Adeena -Adeena

@ Michael: thanks for the link. @Michael:谢谢你的链接。 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: 我添加了以下行:

 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. 所以它正在工作......虽然我不会声称我理解它的工作方式和原因,所以我确实会遇到一些问题,因为我将程序交给其他人使用。 ie, will everyone using the program that has an internet connection be able to open this smtp connection to my server? 即,每个使用具有互联网连接的程序的人都能够打开这个smtp连接到我的服务器吗? I don't know the answer to that... I'll have to wait, see, and learn some more. 我不知道答案......我将不得不等待,看到,并学习更多。

Thanks! 谢谢! :) :)

-Adeena -Adeena

Is the destination address on the same host as your smtp server? 目标地址与smtp服务器位于同一主机上吗? 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. 您使用的SMTP服务器需要是邮件消息的最终目标或邮件交换中的第一个跃点。 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. 例如,如果您从gmail地址向yahoo地址发送邮件,则查看该邮件的第一个邮件服务器必须是您的gmail服务器或yahoo服务器。 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 通过telnet尝试此测试,看看您的smtp服务器是否正常运行: http//www.messagingtalk.org/content/470.html

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("x@gmail.com", "password");
                msg.From = new MailAddress("x@gmail.com");
                msg.To.Add("y@gmail.com");
                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";
            }
        }



    }
}

Check your firewall. 检查防火墙。 Is 2525 post open? 2525开放后?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM