java GMAIL smtp with attach

Discussion in 'Java' started by Jhovarie, Jan 13, 2011.

  1. Jhovarie

    Jhovarie

    Joined:
    Dec 20, 2010
    Messages:
    4
    Likes Received:
    0
    Hello guys how to modify this code i want to attach file but ican please help here is my code

    import javax.mail.*;
    import javax.mail.internet.*;
    import java.util.Properties;
    public class Main {
    public static void main(String[]args){
    try{
    String SMTP_HOST_NAME = "smtp.gmail.com";
    int SMTP_HOST_PORT = 465;

    String SMTP_AUTH_USER = "myaccount@gmail.com";
    String SMTP_AUTH_PWD = "******";
    String SMTP_MailTo = "mailto@live.com";
    String SMTP_Subject = "this is a subject";
    String SMTP_Body = "this is a body text";

    Properties props = new Properties();
    props.put("mail.transport.protocol", "smtps");
    props.put("mail.smtps.host", SMTP_HOST_NAME);
    props.put("mail.smtps.auth", "true");
    props.put("mail.smtps.quitwait", "false");
    Session mailSession = Session.getDefaultInstance(props);
    mailSession.setDebug(true);
    Transport transport = mailSession.getTransport();
    MimeMessage message = new MimeMessage(mailSession);
    message.setSubject(SMTP_Subject);

    message.setContent(SMTP_Body, "text/plain"); // text/html
    message.addRecipient(Message.RecipientType.TO,
    new InternetAddress(SMTP_MailTo));
    transport.connect
    (SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);
    transport.sendMessage(message,
    message.getRecipients(Message.RecipientType.TO));
    transport.close();
    }catch(Exception e){}
    }
    }


    thanks in advance
     
    Jhovarie, Jan 13, 2011
    #1
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.