Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: java GMAIL smtp with attach

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default java GMAIL smtp with attach

    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 = "";
    String SMTP_AUTH_PWD = "******";
    String SMTP_MailTo = "";
    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


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: java GMAIL smtp with attach

    Have a play with this example:

    import java.util.Date;
    import java.util.Properties;
    import javax.activation.DataHandler;
    import javax.activation.FileDataSource;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Multipart;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMultipart;
     
    public class EmailAttachmentDemo {
     
    	/**
    	 * JavaProgrammingForums.com
    	 */
     
        public static void main(String[] args) {
            EmailAttachmentDemo demo = new EmailAttachmentDemo();
            demo.sendEmail();
        }
     
        public void sendEmail() {
            String from = "me@localhost";
            String to = "me@localhost";
            String subject = "Important Message";
            String bodyText = "This is a important message with attachment";
            String filename = "message.pdf";
     
            Properties properties = new Properties();
            properties.put("mail.smtp.host", "localhost");
            properties.put("mail.smtp.port", "25");
            Session session = Session.getDefaultInstance(properties, null);
     
            try {
                MimeMessage message = new MimeMessage(session);
                message.setFrom(new InternetAddress(from));
                message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
                message.setSubject(subject);
                message.setSentDate(new Date());
     
                //
                // Set the email message text.
                //
                MimeBodyPart messagePart = new MimeBodyPart();
                messagePart.setText(bodyText);
     
                //
                // Set the email attachment file
                //
                MimeBodyPart attachmentPart = new MimeBodyPart();
                FileDataSource fileDataSource = new FileDataSource(filename) {
                    @Override
                    public String getContentType() {
                        return "application/octet-stream";
                    }
                };
                attachmentPart.setDataHandler(new DataHandler(fileDataSource));
                attachmentPart.setFileName(filename);
     
                Multipart multipart = new MimeMultipart();
                multipart.addBodyPart(messagePart);
                multipart.addBodyPart(attachmentPart);
     
                message.setContent(multipart);
     
                Transport.send(message);
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java GMAIL smtp with attach

    Quote Originally Posted by JavaPF View Post
    Have a play with this example:

    import java.util.Date;
    import java.util.Properties;
    import javax.activation.DataHandler;
    import javax.activation.FileDataSource;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Multipart;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMultipart;
     
    public class EmailAttachmentDemo {
     
    	/**
    	 * JavaProgrammingForums.com
    	 */
     
        public static void main(String[] args) {
            EmailAttachmentDemo demo = new EmailAttachmentDemo();
            demo.sendEmail();
        }
     
        public void sendEmail() {
            String from = "me@localhost";
            String to = "me@localhost";
            String subject = "Important Message";
            String bodyText = "This is a important message with attachment";
            String filename = "message.pdf";
     
            Properties properties = new Properties();
            properties.put("mail.smtp.host", "localhost");
            properties.put("mail.smtp.port", "25");
            Session session = Session.getDefaultInstance(properties, null);
     
            try {
                MimeMessage message = new MimeMessage(session);
                message.setFrom(new InternetAddress(from));
                message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
                message.setSubject(subject);
                message.setSentDate(new Date());
     
                //
                // Set the email message text.
                //
                MimeBodyPart messagePart = new MimeBodyPart();
                messagePart.setText(bodyText);
     
                //
                // Set the email attachment file
                //
                MimeBodyPart attachmentPart = new MimeBodyPart();
                FileDataSource fileDataSource = new FileDataSource(filename) {
                    @Override
                    public String getContentType() {
                        return "application/octet-stream";
                    }
                };
                attachmentPart.setDataHandler(new DataHandler(fileDataSource));
                attachmentPart.setFileName(filename);
     
                Multipart multipart = new MimeMultipart();
                multipart.addBodyPart(messagePart);
                multipart.addBodyPart(attachmentPart);
     
                message.setContent(multipart);
     
                Transport.send(message);
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }
    }
    Hello i already use it and modify it to gmail but something is missing where should i insert my password?

  4. #4
    Junior Member
    Join Date
    Dec 2010
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java GMAIL smtp with attach

    this link j4guiang.blogspot.com: javamail gmail send mail with attach file is the solution for this thread.

Similar Threads

  1. JavaMail and Gmail
    By khushbu in forum Java Servlet
    Replies: 2
    Last Post: May 21st, 2010, 01:11 AM