How do I send an email message from my C# application?
Asked Answered
S

4

6

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

Sitwell answered 14/12, 2008 at 15:3 Comment(0)
S
7

@ 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:

 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)

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

Sitwell answered 14/12, 2008 at 16:26 Comment(1)
Some SMTP servers require usernames and passwords - which is what you changed and solved the problem. Many SMTP servers, my ISP's for example, don't require passwords because they recognize the origin IP as coming from their ISP's subzone.Aesthetics
V
3

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

Vernon answered 14/12, 2008 at 15:12 Comment(0)
R
0

Check your firewall. Is 2525 post open?

Rowenarowland answered 26/5, 2009 at 10:56 Comment(0)
S
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("[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";
            }
        }



    }
}
Spermatium answered 5/6, 2013 at 11:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.