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

时间:2021-10-10 21:25:59

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

4 个解决方案

#1


7  

@ Michael: thanks for the link. It's very helpful.

@Michael:谢谢你的链接。这非常有帮助。

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. 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.

所以它正在工作......虽然我不会声称我理解它的工作方式和原因,所以我确实会遇到一些问题,因为我将程序交给其他人使用。即,每个使用具有互联网连接的程序的人都可以打开这个smtp连接到我的服务器吗?我不知道答案......我将不得不等待,看到,并学到更多。

Thanks! :)

-Adeena

#2


3  

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

#3


0  

Check your firewall. Is 2525 post open?

检查你的防火墙。 2525开放后?

#4


0  

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";
            }
        }



    }
}

#1


7  

@ Michael: thanks for the link. It's very helpful.

@Michael:谢谢你的链接。这非常有帮助。

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. 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.

所以它正在工作......虽然我不会声称我理解它的工作方式和原因,所以我确实会遇到一些问题,因为我将程序交给其他人使用。即,每个使用具有互联网连接的程序的人都可以打开这个smtp连接到我的服务器吗?我不知道答案......我将不得不等待,看到,并学到更多。

Thanks! :)

-Adeena

#2


3  

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

#3


0  

Check your firewall. Is 2525 post open?

检查你的防火墙。 2525开放后?

#4


0  

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";
            }
        }



    }
}