Thursday, July 28, 2011

Send an Email via SMTP

Send an Email via SMTP
SMTP stands for Simple Mail Transport Protocol and is a simple, well-defined protocol for exchanging emails with an SMTP server. Thankfully, the specifics of the protocol are handled for you in the System.Net.Mail.SmtpClient class. Figure 1 shows an SMTP client.
Figure 1. It’s very easy to create a simple SMTP client with full attachment support.
To see a full example of a simple email program, look at the EmailClient code example for this chapter. Here is just the method for sending email:

private void SendEmail(string host, int port,
string username, string password,
string from, string to,
string subject, string body,
ICollection attachedFiles)
{
//A MailMessage object must be disposed!
using (MailMessage message = new MailMessage())
{
message.From = new MailAddress(from);
message.To.Add(to);
message.Subject = subject;
message.Body = body;
foreach (string file in attachedFiles)
{
message.Attachments.Add(new Attachment(file));
}

SmtpClient client = new SmtpClient(host, port);
//if your SMTP server requires a password,
//the following line is important
client.Credentials = new NetworkCredential(username, password);
//this send is synchronous. You can also choose
//to send asynchronously
client.Send(message);
}
}
*********************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Utilities;



namespace SMTP_Mail1
{

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//MailMessage message= new MailMessage ();
//message.From =new MailAddress("re@ovi.com");
//message.To.Add(new MailAddress("hi@gmail.com"));
//message.Subject ="Sample Mail";
//message .IsBodyHtml =true ;
//message .Body =" Message to be sent";
//SmtpClient objSMTP =new SmtpClient ("DEV2.Company.com", "25");
//objSMTP .UseDefaultCredentials =true ;
//objSMTP .Send (message );

//MailMessage mailMsg = new MailMessage
// (new MailAddress("spr@gmail.com"), new MailAddress("sh@yahoo.com"));
//mailMsg.Subject = "Sending mail through com Account";
//mailMsg.IsBodyHtml = true;
//mailMsg.Body = "Sening mail through o from asp.net";
//System.Net.NetworkCredential Nc = new System.Net.NetworkCredential("spr@gmail.com", "123456");
//SmtpClient smtpClient = new SmtpClient();
//smtpClient.EnableSsl = true;
//smtpClient.UseDefaultCredentials = false;
//smtpClient.Credentials = Nc;

//smtpClient.Host = "smtp.gmail.com";
//smtpClient.Port = 625;
//smtpClient.Send(mailMsg);
//Response.Write("Mail Successfully sent");




//MailMessage mail = new MailMessage();
//mail.From = new MailAddress("ramesh@ex.com");
//mail.To.Add("hi@.com");
//mail.Subject = "This is an email";
//mail.Body = "this is the bodyof the email";
//SmtpClient smtp = new SmtpClient("127.0.0.1");
//smtp.Credentials = new NetworkCredential("rames","12345");
//smtp.Send(mail);

}
}
}

//public static void CreateTestMessage1(string server, int port)
// {
// string to = "jane@contoso.com";
// string from = "ben@contoso.com";
// string subject = "Using the new SMTP client.";
// string body = @"Using this new feature, you can send an e-mail message from an application very easily.";
// MailMessage message = new MailMessage(from, to, subject, body);
// SmtpClient client = new SmtpClient(server, port);
// // Credentials are necessary if the server requires the client
// // to authenticate before it will send e-mail on the client's behalf.
// client.Credentials = CredentialCache.DefaultNetworkCredentials;

// try {
// client.Send(message);
// }
// catch (Exception ex) {
// Console.WriteLine("Exception caught in CreateTestMessage1(): {0}",
// ex.ToString() );
// }
// }

//private void SendEmail()
//{

// AOSMTPLib.MailClass oSmtp = new AOSMTPLib.MailClass();
// //If you don't have a SMTP server, use the following code
// //send email via DNS lookup, ANSMTP lookups SMTP server automatically.
// oSmtp.ServerAddr = "";

// oSmtp.FromAddr = "test@emailarchitect.net";
// 'Correct, both support@adminsystem.net and test@adminsystem.net
// 'belong to adminsystem.net
// oSmtp.AddRecipient( "Support Team", "support@adminsystem.net", 0 );
// oSmtp.AddRecipient( "Tester", "test@adminsystem.net", 1 );

// 'Incorrect, support@adminsystem.net and test@hotmail.com don't belong
// 'to the same domain.
// 'oSmtp.AddRecipient( "Support Team", "support@adminsystem.net", 0 );
// 'oSmtp.AddRecipient( "Tester", "test@hotmail.com", 1 );

// oSmtp.Subject = "Test";
// oSmtp.BodyText = "Hello, this is a test....";

// if( oSmtp.SendMail() == 0 )
// Console.WriteLine( "Message delivered!" );
// else
// Console.WriteLine( oSmtp.GetLastErrDescription());
//}
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
http://programming4.us/enterprise/2354.aspx

Sending mails using Asp.net
http://www.codeproject.com/KB/aspnet/EmailApplication.aspx
http://www.worldofasp.net/tut/Email_ASP.NET2/Sending_Email_in_ASPNET_20_83.aspx
http://www.codeproject.com/KB/aspnet/smtp_mail.aspx

http://weblogs.asp.net/jalpeshpvadgama/archive/2010/12/29/sending-mail-with-gmail-account-using-

system-net-mail-in-asp-net.aspx

http://www.codeproject.com/KB/aspnet/Send_Emails_in_ASPNET20.aspx

http://www.dev4side.com/community/blog/2010/3/2/send-e-mail-messages-using-sharepoint-smtp.aspx


Sending Mail
http://www.directsharepoint.com/2011/03/send-mail-through-code-in-sharepoint.html

http://www.systemnetmail.com/forums/forum.aspx?forum=2
http://thehumblecoder.wordpress.com/2007/01/04/systemnetmail-basic-example/
http://snahta.blogspot.com/2009/05/configuring-smtp-using-webconfig.html

http://digsharepoint.blogspot.com/2011/04/configuring-incoming-email-in.html
http://www.directsharepoint.com/2011/03/send-mail-through-code-in-sharepoint.html
http://digsharepoint.blogspot.com/2011/04/configuring-outgoing-email-in.html

http://www.codeproject.com/Articles/125119/Configuring-Sharepoint-2010-to-Accept-Incoming-Ema
http://www.sharepointology.com/development/how-to-create-alerts-programmatically/


http://blog.furuknap.net/send-a-sharepoint-document-library-file-as-email

http://www.dev4side.com/community/blog/2010/3/2/send-e-mail-messages-using-sharepoint-smtp.aspx
http://www.aspnetemail.com/Examples.aspx

No comments:

Blog Archive