Showing posts with label c# .net mail smtp. Show all posts
Showing posts with label c# .net mail smtp. Show all posts

Thursday, March 15, 2012

C# code for sending mail automatically through application code.

You can generally find this kind of programming requirements in automated mail response system like websites for online booking for booking confirmation mail. This is very basic piece of code. For enterprise level requirement you can use MS SQL server 2008 which has functionality for the same available.


These namespaces are to added to the code.
using System.Net.Mail;
using System.Net;
The following lines of code shows how the mail message is generated and finally send using SMTP.
MailMessage mM = new MailMessage();
//Mail Address
mM.From = new MailAddress("sender@hotmail.com ");
//receiver email id
mM.To.Add("receiver@hotmail.com ");
//subject of the email
mM.Subject = “subject string”;
//attachment
mM.Attachments.Add(new Attachment(@"local path"));
//body of the email
mM.Body = “email body”;
mM.IsBodyHtml = true;
//SMTP client
SmtpClient sC = new SmtpClient("smtp.live.com");
//port number for Hot mail
sC.Port = 25;
//credentials to login in to hotmail account
sC.Credentials = new NetworkCredential("sender@hotmail.com", "password");
//enabled SSL
sC.EnableSsl = true;
//Send an email
sC.Send(mM);

Tuesday, October 18, 2011

Send an E-Mail Given the SMTP Address of an Account

//Namespaces to use


using Outlook = Microsoft.Office.Interop.Outlook;
using System;
using System.Text;
using Outlook = Microsoft.Office.Interop.Outlook;
// code to send mail
namespace OutlookAddIn1
{
    class Sample
    {
        public static void SendEmailFromAccount(Outlook.Application application, string subject, string body, string to, string smtpAddress)
        {

            // Create a new MailItem and set the To, Subject, and Body properties.
            Outlook.MailItem newMail = (Outlook.MailItem)application.CreateItem(Outlook.OlItemType.olMailItem);
            newMail.To = to;
            newMail.Subject = subject;
            newMail.Body = body;

            // Retrieve the account that has the specific SMTP address.
            Outlook.Account account = GetAccountForEmailAddress(application, smtpAddress);
            // Use this account to send the e-mail.
            newMail.SendUsingAccount = account;
            newMail.Send();
        }


        public static Outlook.Account GetAccountForEmailAddress(Outlook.Application application, string smtpAddress)
        {

            // Loop over the Accounts collection of the current Outlook session.
            Outlook.Accounts accounts = application.Session.Accounts;
            foreach (Outlook.Account account in accounts)
            {
                // When the e-mail address matches, return the account.
                if (account.SmtpAddress == smtpAddress)
                {
                    return account;
                }
            }
            throw new System.Exception(string.Format("No Account with SmtpAddress: {0} exists!", smtpAddress));
        }

    }
}

Wednesday, June 29, 2011

SmtpMail and Mail Message

using System.Web.Util 
sendmail()
{
            MailMessage mailMsg = new MailMessage();
            mailMsg .From = "
from@fromServer.com";
            mailMsg .To = "
to@toServer.com";
            mailMsg .Cc = "
cc@ccServer.com"";
            mailMsg .Bcc = "
bcc@bccServer.com";
            mailMsg .Subject = "SubjectOfTheMailString";
            mailMsg .Body = "BodyOfTheMailString";
            SmtpMail.Send(mailMsg ); 

}

Featured Posts

Ubuntu 26.04 LTS: Setting Up Native RDP Remote Login from Windows — A Real-World Troubleshooting Guide

Table of Contents Why use native GNOME RDP? Remote Login vs Desktop Sharing Check U...