Wednesday, January 24, 2007

SENDING EMAIL USING ASP.NET 2.0

Just thought to share my code which i used to implement a simple Mail System. Hope this might help you!

Imports System.Net.Mail

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SendEmail.Click
Dim Msg As MailMessage = New MailMessage()
Dim MailObj As New SmtpClient
Try
Msg.From = New MailAddress(UsersEmail.Text, "Andy")
Msg.To.Add(New MailAddress("ammarfassy@gmail.com", "Author"))
MailObj.Host = "192.168.16.70"
MailObj.Port = 25
Msg.IsBodyHtml = "False"
Msg.Body = Body.Text
Msg.Subject = Subject.Text
MailObj.Send(Msg)
Label1.Text = "Email Sent!"
Catch ex As Exception
Label1.Text = ex.Message
End Try
End Sub

Add your IP Address (local) in your SMTP server & under the properies add the same IP address to Relay Restrictions (Grant & deny permissions to relay email through this SMTP virutal server).

The sent email will be a spam email!

extra notes on this:

under web.config under















namespace System.Net.Mail;

string UserName = System.Configuration.ConfigurationManager.AppSettings["smtpUsername"];
string Password = System.Configuration.ConfigurationManager.AppSettings["smtpPassword"];
string Host = System.Configuration.ConfigurationManager.AppSettings["smtpServer"]; string sFrom = "me@doman.com";
string sTo;
string sSubject = "sample subject";
string sBody = "sample html body";

string toAddress = "myaddress@someotherdomin.com,myaddress2@someotherdomin.com";
string msgSubject = sSubject;
string msgBody = sBody;

SmtpClient client = new SmtpClient(Host);
MailAddress from = new MailAddress(sFrom);
MailAddress to = new MailAddress(toAddress);
MailMessage message = new MailMessage(from, to);
message.Priority = MailPriority.High;
message.IsBodyHtml = true;

System.Net.NetworkCredential NTLMAuthentication = new System.Net.NetworkCredential(UserName, Password);
message.Subject = msgSubject;
message.Body = msgBody;
client.UseDefaultCredentials = false;
client.Credentials = NTLMAuthentication;
client.Send(message);

0 comments: