Results 1 to 2 of 2
  1. #1
    Twiebie is offline Member
    Join Date
    Aug 2013
    Posts
    1
    Rep Power
    0

    Default Send Mail with Attachment

    import java.util.*;
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.mail.internet.MimeMessage.*;
    import javax.activation.*;

    public class SendFileEmail
    {
    public static void main(String [] args)
    {

    // Recipient's email ID needs to be mentioned.
    String to = "############################";

    // Sender's email ID needs to be mentioned
    String from = "##################";

    // Assuming you are sending email from localhost
    String host = "########################";

    // Get system properties
    Properties properties = System.getProperties();

    // Setup mail server
    properties.setProperty("mail.smtp.host", host);


    properties.setProperty("mail.user", "###########");
    properties.setProperty("mail.password", "#################");


    // Get the default Session object.
    Session session = Session.getDefaultInstance(properties);

    try{
    // Create a default MimeMessage object.
    MimeMessage message = new MimeMessage(session);

    // Set From: header field of the header.
    message.setFrom(new InternetAddress(from));

    // Set To: header field of the header.
    message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));

    // Set Subject: header field
    message.setSubject("This is the Subject Line!");

    // Create the message part
    BodyPart messageBodyPart = new MimeBodyPart();

    // Fill the message
    messageBodyPart.setText("This is message body");

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

    // Set text message part
    multipart.addBodyPart(messageBodyPart);

    // Part two is attachment
    messageBodyPart = new MimeBodyPart();
    String filename = "C:/Users/******/Documents/LotusInstall.log";
    DataSource source = new FileDataSource(filename);
    message.setDataHandler(new DataHandler(source));
    message.setFileName(filename);



    String bodytext = "Das ist ein Text" ;
    messageBodyPart.setText(bodytext);
    // messageBodyPart.setFileName(filename);

    multipart.addBodyPart(messageBodyPart);

    // Send the complete message parts
    message.setContent(multipart);

    // Send message
    Transport.send(message);
    System.out.println("Sent message successfully....");
    }catch (MessagingException mex) {
    mex.printStackTrace();
    }
    }
    }











    Hei guys ,
    i need some help. This code runs perfect but i want the Attachment NOT in the Body , the Attachment should be on the document , in no field .
    can somebody help me ?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

Similar Threads

  1. Attachment File In Mail(Unknown Format)
    By Zeus in forum New To Java
    Replies: 7
    Last Post: 08-04-2010, 08:08 AM
  2. Deleting an attachment after sending mail
    By modestmj in forum Java Servlet
    Replies: 2
    Last Post: 06-02-2010, 12:52 PM
  3. Mail Attachment as Source
    By vinoopraj in forum Advanced Java
    Replies: 2
    Last Post: 06-15-2009, 07:21 AM
  4. Send / Receive mail and download attachment
    By Thennarasu in forum Advanced Java
    Replies: 1
    Last Post: 05-30-2009, 12:59 AM
  5. Help, Please!!! Can't Send Mail with SmtpClient
    By jfcup in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 11-07-2008, 01:46 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
  •