Results 1 to 3 of 3
  1. #1
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Javamail with attachement

    Hi,

    I have a problem sending a mail with an attachement.
    The mail is generated fine, and is sent, but there is no tekst in the body of the mail and the attachement is unreadable.

    Here is the code:
    Java Code:
                    String Path     = parm.getPath();
                    String host     = parm.getHost();
                    String user     = parm.getUser();
                    String pass     = parm.getPass();
                    String sender   = parm.getSender();
                    String subject  = textSubject.getText();
                    String message  = textMessage.getText();
                    Docutable doc   = DocuTable.Select(fileid);
                    String filename = doc.getFile();
                    File file       = new File (Path + filename);
                    String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
    
                    Properties props = System.getProperties();
                    props.setProperty("mail.smtps.host", host);
                    props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
                    props.setProperty("mail.smtp.socketFactory.fallback", "false");
                    props.setProperty("mail.smtp.port", "465");
                    props.setProperty("mail.smtp.socketFactory.port", "465");
                    props.setProperty("mail.smtps.auth", "true");
    
                    props.put("mail.smtps.quitwait", "false");
    
                    Session session = Session.getInstance(props, null);
                    MimeMessage msg = new MimeMessage(session);
                    try {
                        msg.setFrom(new InternetAddress(sender));
                        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail, false));
                        DataSource source = new FileDataSource(file);  
                        msg.setDataHandler(new DataHandler(source));  
                        msg.setFileName(filename);
                        msg.setSubject(subject);
                        msg.setSentDate(new Date());
                        msg.setText(message, "utf-8");  //Dette kommer ikke med! ??
               
                    SMTPTransport t;
                    try {
                        t = (SMTPTransport)session.getTransport("smtps");
                        t.connect(host, user, pass);
                        t.sendMessage(msg, msg.getAllRecipients());      
                        t.close();
                        } catch (AddressException ex) {
                        Logger.getLogger(SendMail.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (MessagingException ex) {
                            Logger.getLogger(SendMail.class.getName()).log(Level.SEVERE, null, ex);
                        }     
                        } catch (NoSuchProviderException ex) {
                            Logger.getLogger(SendMail.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (MessagingException ex) {
                        Logger.getLogger(SendMail.class.getName()).log(Level.SEVERE, null, ex);
                    }
    Any suggestions on what I am doing wrong?

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Javamail with attachement

    Yep, you need a multipart message. One MimePart is text, i.e. the bodyand the other Part is the attchment. See this tutorial: How do I send email with attachments using the JavaMail API?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

Similar Threads

  1. getting an attachement from MSAccess
    By liluma in forum New To Java
    Replies: 4
    Last Post: 06-09-2011, 09:31 AM
  2. Javamail
    By johniem in forum New To Java
    Replies: 0
    Last Post: 07-14-2010, 03:00 PM
  3. JavaMail API
    By eponcedeleon in forum Advanced Java
    Replies: 1
    Last Post: 02-28-2010, 12:39 AM
  4. Javamail
    By johniem in forum New To Java
    Replies: 1
    Last Post: 01-29-2010, 03:24 PM
  5. JavaMail Jar
    By rummy in forum Advanced Java
    Replies: 1
    Last Post: 01-21-2010, 03:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •