Results 1 to 2 of 2

Thread: Sending e-mails

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

    Default Sending e-mails

    Hi,

    I have writen some code which I use to send emails from my program, but I would like to save the sent message in a .msg file.
    Any ideas how to do this?

    Java Code:
    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", port);
                    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));
                        msg.setSubject(subject);
                        msg.setSentDate(new Date());
                        
                        MimeBodyPart messageBodyPart = new MimeBodyPart();
                        messageBodyPart.setText(message);
                        Multipart multipart = new MimeMultipart();
                        multipart.addBodyPart(messageBodyPart);
                        messageBodyPart = new MimeBodyPart();
                        DataSource source = new FileDataSource(file);  
                        messageBodyPart.setDataHandler(new DataHandler(source)); 
                        messageBodyPart.setFileName(filename);
                        multipart.addBodyPart(messageBodyPart);
                        msg.setContent(multipart);
    
                        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);
                    }

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

    Default Re: Sending e-mails

    Hi again,

    I have added this code:

    Java Code:
                        File f2 = new File(infile);
                        OutputStream out = null;
                        try {
                            out = new FileOutputStream(f2);
                        } catch (FileNotFoundException ex) {
                            Logger.getLogger(SendMail.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        try {
                            msg.writeTo(out);
                            out.close();
                        } catch (IOException ex) {
                            Logger.getLogger(SendMail.class.getName()).log(Level.SEVERE, null, ex);
                        }
    The file is created, but I am not able to open it. Outlook error says it is in use or access denied to the file.
    But there must be something else. Any ideas?

Similar Threads

  1. Replies: 0
    Last Post: 11-07-2011, 10:48 AM
  2. Sending XML
    By zzpprk in forum XML
    Replies: 17
    Last Post: 03-02-2011, 02:00 PM
  3. [Error] Reading mails and replying at the same time
    By ravjot28 in forum Networking
    Replies: 23
    Last Post: 09-24-2010, 09:12 AM
  4. sending mails thru pop3 server.........
    By vrk in forum Advanced Java
    Replies: 1
    Last Post: 03-25-2009, 11:59 PM
  5. Forwarding mails in message/rfc822
    By prithviraj in forum Advanced Java
    Replies: 0
    Last Post: 05-18-2008, 11:30 AM

Posting Permissions

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