Showing posts with label Mail. Show all posts
Showing posts with label Mail. Show all posts

Tuesday, December 3, 2013

How to send mail from outlook using C # Application

Today I am going to share code for sending email from outlook using your C# application. The code is quite simple and easy but you need to setup email account in outlook with proper configuration.
Outlook
Sending email using outlook from your C# application is a good idea. You don’t need to store your email account credential in database / file. You don’t need to check for internet connection (outlook will send email automatically when connected to internet).
For sending email using outlook from your c# application you will need to add Microsoft.Office.Interop.Outlook reference to your project.
1
after adding this reference to project we are ready to write code for sending email.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace OutlookEmailExample
{
    class SendMail
    {
        public bool SendEmail(string Subject, string To, string Body)
        {
            try
            {
                // Create the Outlook application by using inline initialization.
                Outlook.Application oApp = new Outlook.Application();
    
                //Create the new message by using the simplest approach.
                Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
    
                //set basic properties
                oMsg.Subject = Subject;// "This is the subject of the test message";

                oMsg.To = To;

                oMsg.Body = Body; // "This is the text in the message.";

                oMsg.Send(); // sending email


                return true;
            }
            catch (Exception ee)
            {
                throw ee;
            }
        }
    }
}


Above code demonstrate the easiest approach to create and send email using outlook from your c# application. It creates a outlook application object, a mail message sets some properties like subject, to, body and send the email.

Using code: To send email from your application you will need to create an  object of class SendMail and call SendEmail method of the class.

SendMail sm = new SendMail();
sm.SendEmail("mail subject", "recipient@host.com", "mail body");


We can add CC and BCC:

oMsg.CC = "recipient1@host.com";
oMsg.BCC = "recipient2@host.com";


Note: sending email using outlook from application may take long time so use the above code in background thread.

We can also send HTML email message too..

oMsg.BodyFormat = Outlook.OlBodyFormat.olFormatHTML; //setting mail type to HTML
oMsg.HTMLBody = Body; // setting HTML body message



by setting body format to html we can send HTML mail from our email application.

I hope this will help someone. :)

Tuesday, November 26, 2013

Sending Email using JavaMail API

In this post i am going to provide source code for sending email from our java application. There can be many cases when you may need to send email from your application. JavaMail API is used for sending email from java application.

Below code demonstrate how to send email from your Gmail account using JavaMail API. You can edit the source code for sending email from your desired email provider, for doing this you will need to provide smtp host and smtp port for your email provider. Replace the smtphost and smtpPort in the program.

Note: replace username , pass and to with your account’s username, password and to with the email address you want to send email.

You can Download JavaMail API from this link

/**
* SendMail class Demonstrate using JavaMail API to send email
* The code uses gmail account as sender account, you can use any email provider.
* set smtphost and smtpPort for the desired email provider.
*
* Replace username and pass with your account's username and password.
* Replace toAddress with the email address to whom you want to send email.
*/

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

class SendMail
{
String smtphost = "smtp.gmail.com";
String smtpPort ="587";

Session session;
MimeMessage message1;

String username = "your_email_id@host.com";
String pass = "your_pwd";
String to ="toAddress";

public static void main(String args[])
{
SendMail sm = new SendMail();
sm.createMessage();
sm.sendMail(to,"thisissubject","thisismessage");
}

public void createMessage()
{
try
{

//smtp properties
Properties prop = new Properties();
prop.put("mail.smtp.starttls.enable", "true");
prop.put("mail.smtp.host", smtphost);
prop.put("mail.smtp.user", username);
prop.put("mail.smtp.password", pass);
prop.put("mail.smtp.port", smtpPort);
prop.put("mail.smtp.auth", "true");

session = Session.getDefaultInstance(prop);

message1 = new MimeMessage(session);


}
catch(Exception e)
{
System.out.println("Connetction "+e);
}


}

//send mail method
public void sendMail(String receiptentAddress,String Subject,String messageText)
{
//counstructor will be called
String sendto = receiptentAddress;
String msg = messageText;
String sub = Subject;
String frm = username;
String pas = pass;

try
{
message1.setFrom(new InternetAddress(frm));

message1.addRecipient(Message.RecipientType.TO, new InternetAddress (sendto));

message1.setSubject(sub);

message1.setText(msg);

Transport transport = session.getTransport("smtp");

transport.connect(smtphost, frm, pas);
System.out.println("Sending mail..");
transport.sendMessage(message1, message1.getAllRecipients());

transport.close();

System.out.println("Sent!");

}catch(Exception e)
{
System.out.println(e);
}
}
}