"sending email via gmail smtp server in java" Code Answer


here i am giving some changes, that work fine for me:

session session = session.getinstance(props,null);

you instantiate message object as you did. and finally:

transport transport = session.gettransport("smtp");
string mfrom = "yourgmailusernamewithout@"// example laabidiraissi 
transport.connect("smtp.gmail.com", mfrom, "thepassword");
transport.sendmessage(message, message.getallrecipients());

edit, would you please give me a favor and copy/paste and try this example and show what it displays:

package com.test;

import java.util.properties;

import javax.mail.bodypart;
import javax.mail.message;
import javax.mail.messagingexception;
import javax.mail.multipart;
import javax.mail.session;
import javax.mail.transport;
import javax.mail.internet.addressexception;
import javax.mail.internet.internetaddress;
import javax.mail.internet.mimebodypart;
import javax.mail.internet.mimemessage;
import javax.mail.internet.mimemultipart;

import org.junit.test;

public class emailservice {

public void test(){
    properties props = system.getproperties();
    props.put("mail.smtp.starttls.enable", true); // added this line
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.user", "username");
    props.put("mail.smtp.password", "password");
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.auth", true);

    session session = session.getinstance(props,null);
    mimemessage message = new mimemessage(session);

    system.out.println("port: "+session.getproperty("mail.smtp.port"));

    // create the email addresses involved
    try {
        internetaddress from = new internetaddress("username");
        message.setsubject("yes we can");
        message.addrecipients(message.recipienttype.to, internetaddress.parse("receivermail"));

        // create a multi-part to combine the parts
        multipart multipart = new mimemultipart("alternative");

        // create your text message part
        bodypart messagebodypart = new mimebodypart();
        messagebodypart.settext("some text to send");

        // add the text part to the multipart

        // create the html part
        messagebodypart = new mimebodypart();
        string htmlmessage = "our html text";
        messagebodypart.setcontent(htmlmessage, "text/html");

        // add html part to multi part

        // associate multi-part with message

        // send message
        transport transport = session.gettransport("smtp");
        transport.connect("smtp.gmail.com", "username", "password");
        system.out.println("transport: "+transport.tostring());
        transport.sendmessage(message, message.getallrecipients());

    } catch (addressexception e) {
        // todo auto-generated catch block
    } catch (messagingexception e) {
        // todo auto-generated catch block
By Jamie Counsell on September 21 2022

Answers related to “sending email via gmail smtp server in java”

Only authorized users can answer the Search term. Please sign in first, or register a free account.