Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

Help with sending email notification

Hi,

When a user saves a records in the xml files, I would lile to receive an email notification, how can I acoomplish this with ASP.NET?

Thanks,

Victor
Avatar of NCIT
NCIT

C# solution, could easily be made to vb.net
......

detect the change and whatever values you need to send yourself as var x...

sendEmail(x);

..............
using System.Net.Mail;

public static void sendEmail(string body)
{
                MailMessage msg = new MailMessage();
                msg.To.Add(new MailAddress("to email address goes here"));
                msg.IsBodyHtml = true;
                msg.Body = String.Format("<strong>{0}</strong>",body); // (any html string)
                msg.Subject = "Subject goes here";
                msg.Priority = MailPriority.Normal;

                using (SmtpClient client = new SmtpClient())
                {
                    client.Send(msg);
                }
}



Put you credentials in your web.config
....
<configuration>
.......
  <system.net>
    <mailSettings>
      <smtp from="from email address goes here">
        <network enableSsl="false" host="mail server goes here" port="25" userName="username goes here" password="password goes here" />
      </smtp>
    </mailSettings>
  </system.net>
</configuration>

.......
Avatar of Victor  Charles

ASKER

Thanks, will try to convert to VB.NET and get back to you. I am using local server that comes with ASP,NET and it doesn't come with username and password, can it be done using locsl server that comes with ASP.NET
You can use a web mail account like gmail or yahoo in your config settings I believe... This will avoid the hassle of setting up a local smtp server etc... I have only tested with smtp servers in networks though. As long as your web mail provider allows relay mail I think it should work the same.

Also you can do it without storing your credentials in web.config but that is less secure.
You would use System.Net.NetworkCredentials("me@gmail.com","mypass")

Here is a rough vb.net translation since you said you use that...

Public Sub sendMail(ByVal body as String)
        Dim msg As MailMessage = New MailMessage()
        msg.To.Add("to email goes here")
        msg.From = New MailAddress("me@gmail.com")
        msg.Subject = "subject goes here"
        msg.Body = body
        msg.IsBodyHtml = True
        Dim smtp As SmtpClient = New SmtpClient()
        smtp.Host = "smtp.gmail.com"
        'Only use this credential if you can't figure out the web.config but I would advise using web.config
        smtp.Credentials = New System.Net.NetworkCredential("me@gmail.com", "password goes here")
        smtp.EnableSsl = True
        smtp.Send(msg)
End Sub
Hi,

I tried the code below using my actual emai address and password from my hotmail account but getting error message:

"SmtpException was unhandled by bu user code"

On line: smtp.Send(msg)

How do I fix this error?


Thanks




 Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim body As String
        body = "Testing"
        Dim msg As MailMessage = New MailMessage()
        msg.To.Add("email@hotmail.com")
        msg.From = New MailAddress("email@Hotmail.com")
        msg.Subject = "Test"
        msg.Body = body
        msg.IsBodyHtml = True
        Dim smtp As SmtpClient = New SmtpClient()
        smtp.Host = "smtp.gmail.com"
        'Only use this credential if you can't figure out the web.config but I would advise using web.config
        smtp.Credentials = New System.Net.NetworkCredential("email@Hotmail.com", "psswd")
        smtp.EnableSsl = True
        smtp.Se
I notice you have gmail still in your host setting.
Hotmail outgoing host is "smtp.live.com" I believe
I tried smtp.live.com but getting error message:

No connection could be made because the target machine actively refused it.

Victor
smtp.Port = 587

Ad that and make sure your credentials are correct.
I tried it smtp.Port = 587  and removed smtp.Host = "smtp.live.com" but got the following error: " SMTP host was not specified".

When I tried it with both, I received the following error:

"No connection could be made because the target machine actively refused it"...followed by a bunch of digits

Any ideas what s  could be causing the last error?

Thanks,

Victor
ASKER CERTIFIED SOLUTION
Avatar of NCIT
NCIT

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial