java code to send email using gmail smtp server

Ads
 

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");

} }

Ads









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... by server DEBUG SMTP: mechanism PLAIN not supported by server DEBUG SMTP: mechanism
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
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
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... an error. What does the smtpclient.java code look like? ive checked to see if all libs
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
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
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
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
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
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.
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
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
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
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 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...=" + 9100000000"; // the message center and also how this code is working
java code using swings
java code using swings  code that should be able to enter data of student details using all swings into the access database using jdbc connectivity
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
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
java code using while loop
java code using while loop  

Ads