java code to send email using gmail smtp server

java code to send email using gmail smtp server

please send me the java code to send email using gmail smtp server. and how to send verification code..?

View Answers

November 22, 2012 at 12:11 PM

package servlets;

import java.io.*;

import java.util.Properties;

import javax.mail.*;

import javax.servlet.ServletException;

import javax.servlet.http.*;

public class JavaEmailServlet extends HttpServlet {

Properties emailProperties;
Session mailSession;
MimeMessage emailMessage; 


@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();            
        String toMail = request.getParameter("email");
        String password = request.getParameter("password");      



    try {                        
        JavaEmailServlet javaEmail = new JavaEmailServlet();
        javaEmail.setMailServerProperties();            

        javaEmail.createEmailMessage(toMail) ;
        javaEmail.sendEmail();
    } catch (AddressException ex) {
        System.out.println("ADDRESS EXCEPTION  "+ex);
    } catch (MessagingException ex) {
        System.out.println("MESSAGING EXCEPTION  "+ex);
    }        
    out.println("EMAIL SENT TO YOU MAIL");        
}


public void sendEmail() throws AddressException, MessagingException {

    String emailHost = "smtp.gmail.com";
    String fromUser = "Your Mail id";//just the id alone without @gmail.com
    String fromUserEmailPassword = "your gmail passowrd";

    Transport transport = mailSession.getTransport("smtp");

    transport.connect(emailHost, fromUser, fromUserEmailPassword);
    transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
    transport.close();
    System.out.println("Email sent successfully.");
}

public void createEmailMessage(String toEmails) throws AddressException,
    MessagingException {

    String emailSubject = "no reply-Verification Mail";
    String emailBody = "This is the link from dexter labs<br><a href='http://192.168.3.9:8080/WebApplication1/mail.jsp'>click here</a>";

    mailSession = Session.getDefaultInstance(emailProperties, null);
    emailMessage = new MimeMessage(mailSession);
    emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmails));


    emailMessage.setSubject(emailSubject);
    emailMessage.setContent(emailBody, "text/html");//for a html email
    //emailMessage.setText(emailBody);// for a text email

}

public void setMailServerProperties() {

    String emailPort = "587";//gmail's smtp port

    emailProperties = System.getProperties();
    emailProperties.put("mail.smtp.port", emailPort);
    emailProperties.put("mail.smtp.auth", "true");
    emailProperties.put("mail.smtp.starttls.enable", "true");

} }









Related Tutorials/Questions & Answers:
java code to send email using gmail smtp server
java code to send email using gmail smtp server  please send me the java code to send email using gmail smtp server. and how to send verification code
send mail using smtp in java
send mail using smtp in java  How to send mail using smtp in java?   Sending mail in JSP - SMTP
Advertisements
sending email using smtp in java
sending email using smtp in java  Hi all, I am trying to send and email to through my company mail server. Following is my code package com.tbss... not supported by server DEBUG SMTP: mechanism PLAIN not supported by server DEBUG
how to send sms on mobile and email using java code
how to send sms on mobile and email using java code  hi.... I am developing a project where I need to send a confirmation/updation msg on clients mobile and also an email on their particular email id....plz help me to find
java code to send an email
java code to send an email  i developed one java code that has to send a mail but i am getting an runtime exception calledjavax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465; nested
How to send and recieve email by using java mailserver
How to send and recieve email by using java mailserver  How to send and receive email by using java mail server?   Hi Friend, Please visit the following link: Java Mail Thanks
java source code to send group mails using struts2
java source code to send group mails using struts2  code to send group mails using struts2
SMTP server
SMTP server  Hi How to configure SMTP server.how can send mail using java script ex:I am developing registration page, user register after how can i send mail automatically thanks venu
code for sending email using j2me
code for sending email using j2me  could someone tell me why when i try to compile this line using netbeans 7.1 Properties property... the smtpclient.java code look like? ive checked to see if all libs are imported n
send without authentication mail using java - JavaMail
should be able to send the email. Note: Make sure that your smtp server... to send simple mail without authentication using java. is it possible, could i send email without authentication. I have written following code. public
how to send smtp mail to entered user mail id after user registered using javascript?
how to send smtp mail to entered user mail id after user registered using javascript?  how to send smtp mail to entered user mail id after user registered using javascript
how to send smtp mail to entered user mail id after user registered using javascript?
how to send smtp mail to entered user mail id after user registered using javascript?  how to send smtp mail to entered user mail id after user registered using javascript
This code send email two times but i want only once
This code send email two times but i want only once   public void dbbackup_notify(String email,String data,String subject){ String toEmails = email; Session session=Session.getInstance(props, new
This code send email two times but i want only once
This code send email two times but i want only once   public void dbbackup_notify(String email,String data,String subject){ String toEmails = email; Session session=Session.getInstance(props, new
how to establiish an smtp server ?
how to establiish an smtp server ?  we have an need of establishing an smtp server . "our scenario is that first when the customer enters his... on his email id we have to send him an confirmation mail" . we are haVING
how to send email please give me details with code in jsp,servlet
how to send email please give me details with code in jsp,servlet  how to send email please give me details with code in jsp,servlet
ModuleNotFoundError: No module named 'smtp-send'
ModuleNotFoundError: No module named 'smtp-send'  Hi, My Python... 'smtp-send' How to remove the ModuleNotFoundError: No module named 'smtp... have to install padas library. You can install smtp-send python with following
How to send Email automatically On Daily Basis Using Systems Date.
How to send Email automatically On Daily Basis Using Systems Date.  Hello Sir, I am working on a project in which i have to send emails automatically based on system dates with attchements .Kindly give me the code
To send pdf file to email in java
To send pdf file to email in java   Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger at javax.mail.Session.initLogger(Session.java:283) at javax.mail.Session.
code to send sms alerts using jsp online
code to send sms alerts using jsp online  I am new to mobile aplication development. pls send me the code for sms alerts after clicking the button
send email with attachments in servlet
send email with attachments in servlet  How can we send an email with attachments in servlet?   Send attached email in Java Servlet
sir, how to convert excel file to csv file using java? please send me sample code.
sir, how to convert excel file to csv file using java? please send me sample code.  please send me sample code for converting excel file into csv file uisng java
java mail send using setText()
java mail send using setText()  Hai , Am newly mail send portion in java , Here i write my code , final String userName = "[email protected]); Here am using set Text() method to send mail content in html format. if i use
please send code - Java Beginners
please send code  hai friends plese provide code for fallowing program(CORE JAVA) i am taking string from commandprompt like 'AC2E4T45hj789' now... number it should be taken as single number) URGENTLY send the code
how to send email
how to send email  hii i am new for jsp-servlet and i am making a project in jsp-servlet. i want to send the email after registration to client.please send me code that how to send mail after client registration. thank you
ModuleNotFoundError: No module named 'py-smtp-email'
ModuleNotFoundError: No module named 'py-smtp-email'  Hi, My... 'py-smtp-email' How to remove the ModuleNotFoundError: No module named 'py-smtp-email' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'py-smtp-email'
ModuleNotFoundError: No module named 'py-smtp-email'  Hi, My... 'py-smtp-email' How to remove the ModuleNotFoundError: No module named 'py-smtp-email' error? Thanks   Hi, In your python
Read Email using Java Mail API - James
Read Email using Java Mail API - James   https://www.roseindia.net/javamail/send-mail.shtml From this sample, i understand that we need to pass user credentials for reading the email content. Is there any option to read all
Read Email using Java Mail API - James
Read Email using Java Mail API - James   https://www.roseindia.net/javamail/send-mail.shtml From this sample, i understand that we need to pass user credentials for reading the email content. Is there any option to read all
Read Email using Java Mail API - James
Read Email using Java Mail API - James   https://www.roseindia.net/javamail/send-mail.shtml From this sample, i understand that we need to pass user credentials for reading the email content. Is there any option to read all
Read Email using Java Mail API - James
Read Email using Java Mail API - James   https://www.roseindia.net/javamail/send-mail.shtml From this sample, i understand that we need to pass user credentials for reading the email content. Is there any option to read all
java source code to create mail server using struts2
java source code to create mail server using struts2  java source code to create mail server using struts2
send HTML Email with jsp and servlet
send HTML Email with jsp and servlet  Can You please show me how to send html Email using JSP and Servlet thank you
gmail access using javamail API
gmail access using javamail API  how to coonect to gmail using java mail API ?  Hi Friend, Please visit the following link: http...;Hi, Here is the link of [Java Mail examples][1]. [http://www.roseindia.net
Code to Send SMS From PC to Mobile using Internet
Code to Send SMS From PC to Mobile using Internet  import... !!"); } } I am Using This code to send SMS from PC to Mobile Phone... static void send(String uid, String pwd, String phone, String msg) throws Exception
Code to Send SMS From PC to Mobile using Internet
Code to Send SMS From PC to Mobile using Internet  import... !!"); } } I am Using This code to send SMS from PC to Mobile Phone... static void send(String uid, String pwd, String phone, String msg) throws Exception
Code to Send SMS From PC to Mobile using Internet
Code to Send SMS From PC to Mobile using Internet  import... !!"); } } I am Using This code to send SMS from PC to Mobile Phone... static void send(String uid, String pwd, String phone, String msg) throws Exception
how to send joption panel dialog from server to multipleclients using sockets?
how to send joption panel dialog from server to multipleclients using sockets?  how to send joption panel dialog from server to multipleclients using sockets?like we get a first message with 2 buttonsand on clicking one button we
how to send joption panel dialog from server to multipleclients using sockets?
how to send joption panel dialog from server to multipleclients using sockets?  how to send joption panel dialog from server to multipleclients using sockets?like we get a first message with 2 buttonsand on clicking one button we
how to send joption panel dialog from server to multipleclients using sockets?
how to send joption panel dialog from server to multipleclients using sockets?  how to send joption panel dialog from server to multipleclients using sockets?like we get a first message with 2 buttonsand on clicking one button we
Unable to send mail using php script
Unable to send mail using php script  Hello i am trying to send mail using mail function.But the message is never sent. I have installed a free smtp server which i run in background before executing the code.and my php.ini file
How to send sms alerts to mobile using java?
How to send sms alerts to mobile using java?  Hi i have used the Code to send message on mobile using java code, it is not working - COM2... || []).push({}); and also how this code is working
ModuleNotFoundError: No module named 'send_email'
ModuleNotFoundError: No module named 'send_email'  Hi, My Python... 'send_email' How to remove the ModuleNotFoundError: No module named 'send... have to install padas library. You can install send_email python
Send Email From JSP & Servlet
J2EE Tutorial - Send Email From JSP & Servlet... As for sending mail from webserver, using JavaMail API, the following code shows...  (We are using Tomcat server.  The TOMCAT server is a webserver
send mail using JavaScript
send mail using JavaScript  How can we send mail using JavaScript?   Hi friends, You can not send email directly using JavaScript. But you can use JavaScript to execute a client side email program send the email using
Email Validation code - Java Interview Questions
Email Validation code  Can anybody tell me how to write email validation code using java   Hi Friend, Please visit the following link: http://www.roseindia.net/tutorial/java/core/regularExpressions.html Thanks
send me javascript code - Java Beginners
send me javascript code  please send me code javascript validation code for this html page.pleaseeeeeeeee. a.first:link...          Hi friend, complete code
Send Mail using James Server
Send Mail using James Server       This application illustrates how to send mail on the mail server and also saved the message in database table. In this example we create
How does Email works
): SMTP is used to deliver Email to the recipient mail server. Java mail Application... send email to the address’s email server. When recipient log on his mail...; Each Internet domain has email Server, when a user sends
how to send html email from dreamweaver
how to send html email from dreamweaver  how to send html email from dreamweaver

Ads