Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8063741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:08:43+00:00 2026-06-05T11:08:43+00:00

im using the following code to send email via JavaMail API can some one

  • 0

im using the following code to send email via JavaMail API

can some one guide me how to add attachment in this code so i can sent email with attachment..

package Javamail;

import javax.mail.*;

import javax.mail.internet.*;

import java.util.*;

import java.io.*;

public class SendmailUsejavamail
{

  private static final String SMTP_HOST_NAME = "smtp.gmail.com";

  private static final String SMTP_AUTH_USER = "aaaa@gmail.com";

  private static final String SMTP_AUTH_PWD  = "xxxxxxxxx";


  private static final String emailMsgTxt      = "Online Order Confirmation Message. Also include the Tracking Number.";

  private static final String emailSubjectTxt  = "Order Confirmation Subject";

  private static final String emailFromAddress = "bbbbb@gmail.com";

  // Add List of Email address to who email needs to be sent to

  private static final String[] emailList = {"abc@gmail.com", "abcd@gmail.com"};


  public static void main(String args[]) throws Exception{

    SendmailUsejavamail smtpMailSender = new SendmailUsejavamail();

    smtpMailSender.postMail( emailList, emailSubjectTxt, emailMsgTxt, emailFromAddress);

    System.out.println("Sucessfully Sent mail to All Users");

  }


  public void postMail( String recipients[ ], String subject,
                            String message , String from) throws MessagingException
  {

    boolean debug = false;

     //Set the host smtp address
     Properties props = new Properties();

     props.put("mail.smtp.host", SMTP_HOST_NAME);

     props.put("mail.smtp.auth", "true");

    Authenticator auth = new SMTPAuthenticator();

    Session session = Session.getDefaultInstance(props, auth);

    session.setDebug(debug);

    // create a message
    Message msg = new MimeMessage(session);

    // set the from and to address
    InternetAddress addressFrom = new InternetAddress(from);
    msg.setFrom(addressFrom);
    InternetAddress[] addressTo = new InternetAddress[recipients.length];
    for (int i = 0; i < recipients.length; i++)
    {
        addressTo[i] = new InternetAddress(recipients[i]);
    }
    msg.setRecipients(Message.RecipientType.TO, addressTo);
    // Setting the Subject and Content Type
    msg.setSubject(subject);
    msg.setContent(message, "text/plain");
    Transport.send(msg);
 }

/**
* SimpleAuthenticator is used to do simple authentication
* when the SMTP server requires it.
*/

private class SMTPAuthenticator extends javax.mail.Authenticator{

    public PasswordAuthentication getPasswordAuthentication(){

        String username = SMTP_AUTH_USER;

        String password = SMTP_AUTH_PWD;

        return new PasswordAuthentication(username, password);

    }

}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T11:08:45+00:00Added an answer on June 5, 2026 at 11:08 am
     MimeMessage message = new MimeMessage(session);
    
     // create the message part 
      MimeBodyPart messageBodyPart = new MimeBodyPart();
    
     //fill message
      messageBodyPart.setText("Hi");
    
       Multipart multipart = new MimeMultipart();
       multipart.addBodyPart(messageBodyPart);
    
    // Part two is attachment
    messageBodyPart = new MimeBodyPart();
    DataSource source =  new FileDataSource(fileAttachment); // your file
    messageBodyPart.setDataHandler( new DataHandler(source));
    messageBodyPart.setFileName(fileAttachment);
    multipart.addBodyPart(messageBodyPart);
    
    // Put parts in message
    message.setContent(multipart);
    
    // Send the message
    Transport.send( message );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to send an email with an attachment using the following code (Python
I am using the following code to send an email with a pdf attachment:
I am using the following code to send an email attachment. I'm using C#,
I am using the following code to send an email with attachments. I can
I am trying to send email using c# following is my code. try {
I am using the following code to send an email with the contents of
I am using following code to create and send email through CGI perl. The
I'm using Zend_Mail and the following code to send my email messages. $mail =
I'm using the following code to send an email from my vb.net web application.
I'm using the following code from this answer Sending email in .NET through Gmail

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.