Saturday, 11 February 2017

POP3, MIME, FTP Send Email in ASP.NET



<![endif]-->

POP3, MIME, FTP Send Email in ASP.NET




Sending messages with ASP.NET Using POP3, MIME And FTP Servers is quite straight forward. The .NET structure accompanies a whole namespace for taking care of messages, the System.Net.Mail namespace. In the accompanying illustrations, we will utilize two classes from this namespace: The MailMessage class, for the real email, and the SmtpClient class, for sending the email.

As you might know, sends are sent through a SMTP server, and to send sends with the .NET system, you will require access to a SMTP server. In case you're trying things locally, the organization that provisions your with Internet get to, will typically have a SMTP server that you can utilize, and in the event that you wish to utilize one of these cases on your genuine site, the organization that has your site will normally have a SMTP server that you can utilize. Experience the bolster pages to locate the real address - it's normally something along the lines of smtp.your-isp.com or mail.your-isp.com.

When you have an available SMTP server, we're prepared to send our first email. For the main illustration, all you need is a void page, with the accompanying code in the CodeBehind:



protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        MailMessage mailMessage = new MailMessage();
        mailMessage.To.Add("your.own@mail-address.com");
        mailMessage.From = new MailAddress("another@mail-address.com");
        mailMessage.Subject = "ASP.NET e-mail test";
        mailMessage.Body = "Hello world,\n\nThis is an ASP.NET test e-mail!";
        SmtpClient smtpClient = new SmtpClient("smtp.your-isp.com");
        smtpClient.Send(mailMessage);
        Response.Write("E-mail sent!");
    }
    catch(Exception ex)
    {
        Response.Write("Could not send the e-mail - error: " + ex.Message);
    }
}


That is quite you have to send an email. We make another MailMessage occurrence, include another collector, set the "From" address and the subject, and after that we compose a straightforward test message for the body of the email. From that point forward, we make another occurrence of the SmtpClient, with the host address of the SMTP server that you may use as a parameter, and after that we utilize the SmtpClient case to shoot the email out into the internet. The whole thing is encompassed by a try..catch piece, just on the off chance that something turns out badly.

This was only an exceptionally essential illustration, yet there are a ton of different choices. Here is a short rundown with fascinating thoughts:


You can join one or a few documents, essentially by adding them to the Attachments accumulation
mailMessage.Attachments.Add(new Attachment(Server.MapPath("~/image.jpg")));


You can send to more than one individual in the meantime, just by adding another email deliver to the "To" gathering, this way:
mailMessage.To.Add("your.own@mail-address.com");
mailMessage.To.Add("another@mail-address.com");


You can set a name for the sender - something else, just the email address will be appeared in the "From" section of the collectors email customer. For example, similar to this:
mailMessage.From = new MailAddress("me@mail-address.com", "My Name");


You can send HTML messages, rather than the default plaintext sends, to utilize more confounded designs. Here is a straightforward illustration:
mailMessage.IsBodyHtml = true;
mailMessage.Body = "Hello <b>world!</b>";


You can utilize the CC and BCC fields, much the same as in consistent email messages, this way:
mailMessage.CC.Add("me@mail-address.com");
mailMessage.Bcc.Add("me2@mail-address.com");


You can set the need of an email, this way:

mailMessage.Priority = MailPriority.High;

Previous Post
Next Post

0 comments: