• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 336
  • Last Modified:

Need my ASP.NET MVC site to send an email

Instead of needing a listener, I could insert a line of code in one function to notify my BlackBerry anytime my program throws an exception. So please provide me the code (or link to the code) so I can have my C# program send an outbound email.

Thanks,
newbieweb
0
newbieweb
Asked:
newbieweb
  • 4
  • 3
3 Solutions
 
newbiewebSr. Software EngineerAuthor Commented:
I configured the attached code but the email is not going out. Is doesn not throw an exception but I never get an email.

for txtFrom I am using my working email address.  Is that right or should it be a mail server??
private void SendEmail(MailAddressCollection txtTo, MailAddress txtFrom, string txtSubject, string txtBody)
        {
            var mMailMessage = new System.Net.Mail.MailMessage();

            foreach(MailAddress ma in txtTo)
            {
                mMailMessage.To.Add(ma);
            }
            mMailMessage.From = txtFrom;
            mMailMessage.Subject = txtSubject;
            mMailMessage.Body = txtBody;
            mMailMessage.IsBodyHtml = true;
            mMailMessage.Priority = System.Net.Mail.MailPriority.Normal;

            SmtpClient mSmtpClient = new SmtpClient();
            mSmtpClient.Send(mMailMessage);
        }

Open in new window

0
 
Carl TawnSystems and Integration DeveloperCommented:
Unless the machine hosting your app is also a mail server then you need to supply the address/ip of your mail server (and possibly login credentials too)
0
 
newbiewebSr. Software EngineerAuthor Commented:
What properties of mMailMessage do I need to set?
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Carl TawnSystems and Integration DeveloperCommented:
None. The SmtpClient object is the proxy between your code and your mail server, so it needs the details:
SmtpClient mSmtpClient = new SmtpClient("your mail server", 25);
mSmtpClient.Send(mMailMessage);

Open in new window

If you need to supply credentials to the mail server you need to add the following before sending:
smtp.Credentials = new System.Net.NetworkCredential("your_username", "your_password");

Open in new window

0
 
newbiewebSr. Software EngineerAuthor Commented:
Where do I add the smtp object?
0
 
Carl TawnSystems and Integration DeveloperCommented:
Oops, that should have said:
mSmtpClient.Credentials = new System.Net.NetworkCredential("your_username", "your_password");

Open in new window

0
 
newbiewebSr. Software EngineerAuthor Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now