Results 1 to 3 of 3
  1. #1
    eponcedeleon is offline Member
    Join Date
    Feb 2010
    Posts
    16
    Rep Power
    0

    Default setting content in email with attachment JavaMail

    I am able to send email with and without attachments...however when i send an email with an attachment i cannot set anything in the content of the email...

    this is the line that with without attachment works
    message.setContent("This is a test.", "text/plain");

    but when i have an attachment i enable this line and it does not set that content to the email
    attachFilePart.setText("This is a test from attachment");

    and havent nad any succes....ANY IDEAS??

    Java Code:
    	public void send(String attachment, String to) throws Exception {
    		Properties props = new Properties();
    
    		props.put("mail.transport.protocol", "smtp");
    		props.put("mail.smtp.auth", "true");
    		props.put("mail.smtp.quitwait", "false");
    
    		Session mailSession = Session.getDefaultInstance(props);
    		mailSession.setDebug(true);
    		Transport transport = mailSession.getTransport();
    
    		MimeMessage message = new MimeMessage(mailSession);
    		message.setFrom(new InternetAddress("Snapshot Integration <snapshot@blackbrd.com.mx>"));
    		message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
    		message.setSubject("Snapshot Controller");
    		//		message.setContent("This is a test.", "text/plain");
    
    		//ATTACHMENT
    		MimeBodyPart attachFilePart = new MimeBodyPart();
    		FileDataSource fds = new FileDataSource(attachment);
    		attachFilePart.setDataHandler(new DataHandler(fds));
    		attachFilePart.setFileName(fds.getName());
    		attachFilePart.setText("This is a test from attachment");
    		Multipart mp = new MimeMultipart();
    		mp.addBodyPart(attachFilePart);
    		message.setContent(mp);
    		transport.connect(SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);
    		transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
    
    		transport.close();
    Last edited by eponcedeleon; 03-02-2010 at 09:51 PM. Reason: changing title

  2. #2
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    366
    Rep Power
    6

    Default

    Use Search on this forum
    and find many examples for mail sending with/without attachments.

    For any question related to mail API visit:

    JavaMail API - FAQ

    regards

  3. #3
    eponcedeleon is offline Member
    Join Date
    Feb 2010
    Posts
    16
    Rep Power
    0

Similar Threads

  1. sending email
    By kkk in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 08-21-2009, 11:20 AM
  2. [SOLVED] Sending Email via UNIX
    By nwboy74 in forum Networking
    Replies: 4
    Last Post: 03-25-2009, 12:31 AM
  3. Need help sending an email
    By isshino in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-19-2009, 09:35 PM
  4. sending email without SMTP?
    By java_srinivasan in forum Advanced Java
    Replies: 1
    Last Post: 07-23-2008, 04:45 PM
  5. Sending mail Using JAVAMAIL
    By peiceonly in forum Advanced Java
    Replies: 3
    Last Post: 07-19-2007, 03:11 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
  •