How to send a mail using java code in eclipse

  • by

What you will learn here about java Email send

  • How to send a mail using java code in eclipse
  • java send email with attachment

How to send a mail using java code in eclipse

Please follow the following steps to know how to send a mail using java code in eclipse
1)Please first sign in first your Gmail or Email account in the browser.

Please make sure that you have signed in to your Gmail account form which you want to send the email

2)Now please click on the following link to enable security option so that you can send email using python

Gmail allows the app to send email

java send Email gmail

Please make sure that you have followed the above step. Without the above step, you will not be able to send an Email using python

3)Now please click on the following link to enable the captcha option so that you can send an email using python without login the browser.

Gmail unlock captcha to send email

Email send using java

Please make sure that you have followed the above step. If you have followed the above step then you can send Email without sign in the browser. If not then you have to sign in browser to send the email.

4)Please download the following code to send Email using java
Java email send code (20 downloads)

5)Now please unzip the downloaded code

6)Open unzip code with your Eclipse IDE

7)Now please open CreateEmail.java file and modify the receiver Email address which is shown below and save the changes
java send Email Gmail

8)Now please open the SendEmail.java file and modify sender email name and password which is shown below and save the changes
send a mail using java code in eclipse

9)Now please run the Email.java file to send Email which is shown below
send email using java code in eclipse
10)Now please check your receiver email to see a sent email which is shown below
How to send a mail using java code in eclipse

java send email with attachment

Please follow the following steps to know how to send Email with attachment in java

please add the following code in the CreateEmail.java file of above downloaded code
			
emailMessage.setSubject("Test Email");
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Please Find the attached file");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
String filename = "C:\\Users\\ABCD\\Desktop\\pic.png";		        
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("pic.png");
multipart.addBodyPart(messageBodyPart);
emailMessage.setContent(multipart);

In the downloaded code please modify the following things:

  • Please comment two lines of CreateEmail.java file which is shown below
  • Please add above code in CreateEmail.java file which is shown below
  • Modify the path of file with respect to your system which is shown below

java send email with attachment