Showing posts with label mail. Show all posts
Showing posts with label mail. Show all posts
Wednesday, January 29, 2014
Thursday, January 10, 2013
Simple java program to send Mail using JavaMail API
The JavaMail API provides a platform-independent and protocol-independent framework to build mail and messaging applications. to know more, click here.
For below program to run, please add mail.jar in your application's classpath. Download mail.jar.
For below program to run, please add mail.jar in your application's classpath. Download mail.jar.
Labels:
API,
mail,
Simple Java Codes
Wednesday, December 7, 2011
How to send mail using MAILX command in UNIX / LINUX
Simple MAILX command
Below is the syntax to send simple mail using mailx command in UNIX / Linux.
You can use it as command line or used it in your shell script.
$ mailx -s "Mail Subject" test@xyz.com
type body of the mail
...
..
EOT (Ctrl+d)
$
Send attachment using MAILX command
In some cases you may have to send attachment in the mail. Lets see the syntax of sending an attachment using mailx command.
Below is the syntax to attach a file while sending a mail using mailx command in LINUX / UNIX
( cat /root/MailBody.txt
uuencode /root/file_name.txt file_name.txt
) | mailx -s "Mail Subject" test@xyz.com
Here file_name.txt is the attachment to be attached in the mail
AND MailBody.txt is the text body of the mail.
Below is the syntax to send simple mail using mailx command in UNIX / Linux.
You can use it as command line or used it in your shell script.
$ mailx -s "Mail Subject" test@xyz.com
type body of the mail
...
..
EOT (Ctrl+d)
$
Send attachment using MAILX command
In some cases you may have to send attachment in the mail. Lets see the syntax of sending an attachment using mailx command.
Below is the syntax to attach a file while sending a mail using mailx command in LINUX / UNIX
( cat /root/MailBody.txt
uuencode /root/file_name.txt file_name.txt
) | mailx -s "Mail Subject" test@xyz.com
Here file_name.txt is the attachment to be attached in the mail
AND MailBody.txt is the text body of the mail.
Monday, March 16, 2009
java : Simple java program to read mails from your gmail account
Connecting GMail using POP3 connection with SSL
POP (Post Office Protocol 3)
This protocol defines a single mailbox for a single user and a standardized way for users to access mailboxes and download messages to their computer.
To run this code u need to download Java Mail API and JavaBeans Activation Framework
/*Here is the complete tested code for ReadGmailAccount.java*/
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.*;
import javax.activation.*;
public class ReadGmailAccount
{
public static void main(String[] args)
{
Properties props = System.getProperties();
props.put("mail.pop3.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback","false");
props.setProperty("mail.pop3.port", "995");
props.setProperty("mail.pop3.socketFactory.port", "995");
Session session = Session.getDefaultInstance(props,null);
URLName urln = new URLName("pop3","pop.gmail.com",995,null,"madan712@gmail.com","*******");//password
try
{
Store store = session.getStore(urln);
store.connect();
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message[] message = folder.getMessages();
for (int i = 0; i < message.length; i++)
{
System.out.println("------------ Message " + (i + 1) + " ------------");
System.out.println("Subject : " + message[i].getSubject());
System.out.println("SentDate : " + message[i].getSentDate());
System.out.println("From : " + message[i].getFrom()[0]);
System.out.print("Message : ");
InputStream stream = message[i].getInputStream();
while (stream.available() != 0)
{
System.out.print((char) stream.read());
}
}
folder.close(true);
store.close();
}
catch (Exception ex)
{
System.out.println("Exception occured"+ex);
ex.printStackTrace();
}
}
}
POP (Post Office Protocol 3)
This protocol defines a single mailbox for a single user and a standardized way for users to access mailboxes and download messages to their computer.
To run this code u need to download Java Mail API and JavaBeans Activation Framework
/*Here is the complete tested code for ReadGmailAccount.java*/
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.*;
import javax.activation.*;
public class ReadGmailAccount
{
public static void main(String[] args)
{
Properties props = System.getProperties();
props.put("mail.pop3.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback","false");
props.setProperty("mail.pop3.port", "995");
props.setProperty("mail.pop3.socketFactory.port", "995");
Session session = Session.getDefaultInstance(props,null);
URLName urln = new URLName("pop3","pop.gmail.com",995,null,"madan712@gmail.com","*******");//password
try
{
Store store = session.getStore(urln);
store.connect();
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message[] message = folder.getMessages();
for (int i = 0; i < message.length; i++)
{
System.out.println("------------ Message " + (i + 1) + " ------------");
System.out.println("Subject : " + message[i].getSubject());
System.out.println("SentDate : " + message[i].getSentDate());
System.out.println("From : " + message[i].getFrom()[0]);
System.out.print("Message : ");
InputStream stream = message[i].getInputStream();
while (stream.available() != 0)
{
System.out.print((char) stream.read());
}
}
folder.close(true);
store.close();
}
catch (Exception ex)
{
System.out.println("Exception occured"+ex);
ex.printStackTrace();
}
}
}
Labels:
mail
Tuesday, March 3, 2009
java : simple program to send mails using Gmail (ie javamail API)
Here is the code to send mail using gmail
/*Here is the complete tested code for Gmail.java */
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class Gmail
{
public static void main(String[] args)
{
Properties props = new Properties();
props.put("mail.smtp.user","madan712@gmail.com");
props.put("mail.smtp.host","smtp.gmail.com");
props.put("mail.smtp.port","465");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth","true");
props.put("mail.smtp.debug","true");
props.put("mail.smtp.socketFactory.port","465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback","false");
try
{
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
MimeMessage msg = new MimeMessage(session);
msg.setSubject("Testing");
msg.setText("Hi !! how are you??");
msg.setFrom(new InternetAddress("madan712@gmail.com"));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress("madan@indiagames.com"));
msg.addRecipient(Message.RecipientType.CC, new InternetAddress("madan_chaudhary@rediffmail.com"));
msg.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect("smtp.gmail.com","madan712@gmail.com","********");//password
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
}
catch (Exception ex)
{
System.out.println("Exception occured");
ex.printStackTrace();
}
}
}
this is common exception every one gets if your server doesn't responds....
Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 25;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
at javax.mail.Service.connect(Service.java:288)
at javax.mail.Service.connect(Service.java:169)
at javax.mail.Service.connect(Service.java:118)
at SimpleMail.main(SimpleMail.java:23)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:232)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1250)
... 5 more
Note: to run this code u need to download JavaMail 1.4.2 jar files
/*Here is the complete tested code for Gmail.java */
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class Gmail
{
public static void main(String[] args)
{
Properties props = new Properties();
props.put("mail.smtp.user","madan712@gmail.com");
props.put("mail.smtp.host","smtp.gmail.com");
props.put("mail.smtp.port","465");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth","true");
props.put("mail.smtp.debug","true");
props.put("mail.smtp.socketFactory.port","465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback","false");
try
{
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
MimeMessage msg = new MimeMessage(session);
msg.setSubject("Testing");
msg.setText("Hi !! how are you??");
msg.setFrom(new InternetAddress("madan712@gmail.com"));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress("madan@indiagames.com"));
msg.addRecipient(Message.RecipientType.CC, new InternetAddress("madan_chaudhary@rediffmail.com"));
msg.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect("smtp.gmail.com","madan712@gmail.com","********");//password
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
}
catch (Exception ex)
{
System.out.println("Exception occured");
ex.printStackTrace();
}
}
}
this is common exception every one gets if your server doesn't responds....
Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 25;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
at javax.mail.Service.connect(Service.java:288)
at javax.mail.Service.connect(Service.java:169)
at javax.mail.Service.connect(Service.java:118)
at SimpleMail.main(SimpleMail.java:23)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:232)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1250)
... 5 more
Labels:
mail
Subscribe to:
Posts (Atom)