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



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

Have Programming Question? Ask it here!
 
 
 


Thirumalesh
java code to send email using gmail smtp server
1 Answer(s)      a year and 11 months 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
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 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
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
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
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
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
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
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
how to send contact detail in email
how to send contact detail in email  hi...all of u.....i am work... problem...frnd how to send a contact form detail mail on a click submit button...%"><b>Email:</b></td> <td width="100%"><input
Java Email
Java Email  I am making one java email applications using jsp-servlets. can you tell me that how can i recieve and send email dynamically in my application in UI... thanx.   Hi, Please read at Email From JSP &
How does Email works
send email to the address’s email server. When recipient log on his mail... to deliver Email to the recipient mail server. Java mail Application communicates...; Each Internet domain has email Server, when a user sends
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
how to send html email from dreamweaver
how to send html email from dreamweaver  how to send html email from dreamweaver
how to send email in php and attech with any file
how to send email in php and attech with any file  how to send email and attech with any file like text, image, video
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
Send email as html with embedded images as background
Send email as html with embedded images as background  Hi, I am developing a PHP mailing application. I have to send image in the email as attachment. Give me solution for sending image in the email data. Thanks
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
code for email - Spring
code for email  i want a java code using springs after login process sending an email to the corresponding with a text message to them as successfully registered  Hi Friend, Please visit the following links: http
email search code in php
email search code in php  email search with multiple option in php
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
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 email list of email address through in database table list
how to send email list of email address through in database table list  package com.com; import java.io.IOException; import java.io.PrintWriter...(); } finally{ } //return true; } } print("code sample
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 to multiple recipients using jsp
Email to multiple recipients using jsp  Hi sir, Am a doing a project,in that i need to send email to multiple user at a time,the to address should enter manually its not not be written in code using jsp. Regards, Santhosh
 

 

 

DMCA.com