Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JavaMail java code to send email using gmail smtp server
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Thirumalesh
java code to send email using gmail smtp server
1 Answer(s)      2 years and a month ago
Posted in : JavaMail


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


Advertisement
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
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
Advertisements
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
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 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
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
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
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 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 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
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
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
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
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 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
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 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
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
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
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 Email
Java Email  i am writting a program to send emails using gmail smtp server. I had the following error: java.lang.ClassFormatError: Absent Code...) Exception in thread "main" Java Result: 1 BUILD SUCCESSFUL (total time: 6 seconds
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
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
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 = "xxxxxx@gmail.com...); 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
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
Send Email From JSP & Servlet
J2EE Tutorial - Send Email From JSP & Servlet... webserver, using JavaMail API, the following code shows how the required... classpath to c:\jsdk2.0\src (java servlet development kit).  (We are using
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
Advertisements
 

 

 

DMCA.com