Results 1 to 5 of 5

Thread: Java mail API

  1. #1
    sagarsbodage is offline sHUGAM
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Default Java mail API

    hello friends,
    i m new to java,
    I just want to use a java mail API to send an email, but without authentication, i tried to set and unset authentication properties and tested but without authentication it's not working. My program is working fine with authentication but I just want it without authentication, so can i get some help,
    Below i am posting my email sending code, which uses a SMTP server to send an email....

    .................................................. .................................................. ..........

    /*
    * Program to send a single file using java mail API.
    */
    Java Code:
    package com.email;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Date;
    import java.util.Properties;
    
    import javax.activation.DataHandler;
    import javax.activation.DataSource;
    import javax.activation.FileDataSource;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Multipart;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMultipart;
    
    public class Email
    {
    	private String to;
    	private String subject;
    	private String body;
    
    	static Properties properties = new Properties();
    	static
    	{
    		properties.put("mail.smtp.port", "465");
    		properties.put("mail.smtp.auth", "true");
    		properties.put("mail.smtp.socketFactory.port", "465");
    		properties.put("mail.smtp.host","ourHostName.com");
    		properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
    
    	}
    
    	public static void main(String[] args) throws IOException 
    	{
    		String message, messageBody = "";
    		Email email = new Email();
    
    		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    
    		System.out.print("To: ");
    		String to = in.readLine();
    		email.setTo(to);
    
    		System.out.print("Subject: ");
    		String subject = in.readLine();
    		email.setSubject(subject);
    
    		System.out.println("Type \"stop\" to end-up with writing a message body:");
    		while (!((message = in.readLine()).equals("stop")))
    		{
    			messageBody += message;
    		}
    
    		email.setBody(messageBody);
    
    		String status = email.sendMail(to, messageBody);
    		System.out.println(status);
    	}
    
    	public String sendMail(String to, String bodyText) throws IOException {
    
    		System.out.println("Trying to send a mail.\nAuthenticating.");
    		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    
    		try {
    			Session session = Session.getDefaultInstance(properties,
    					new javax.mail.Authenticator() {
    						protected PasswordAuthentication getPasswordAuthentication() {
    							return new PasswordAuthentication(
    									"ourEmailId@domainName.com",
    									"password");
    						}
    					});
    
    			System.out.println("Authenticated.");
    
    			MimeMessage message = new MimeMessage(session);
    			message.setFrom(new InternetAddress("admin@domainName.com"));
    			message.setRecipients(Message.RecipientType.TO,
    					InternetAddress.parse(to));
    			message.setSubject(subject);
    
    			MimeBodyPart textPart = new MimeBodyPart();
    			textPart.setText(" " + bodyText);
    
    			Multipart multiPart = new MimeMultipart();
    			multiPart.addBodyPart(textPart);
    
    			MimeBodyPart filePart = new MimeBodyPart();
    
    			System.out.print("Enter file name to attch in an email:");
    			String fname = in.readLine();
    
    			DataSource source = new FileDataSource(fname);
    			filePart.setDataHandler(new DataHandler(source));
    			filePart.setFileName(fname);
    			multiPart.addBodyPart(filePart);
    
    			message.setContent(multiPart);
    			message.setSentDate(new Date());
    			Transport.send(message);
    
    			System.out.println("File also sent successfully.");
    		}
    		catch (MessagingException e)
    		{
    			e.printStackTrace();
    			
    			return " Mail sending failed.";
    		}
    
    		return "Your mail is sent successfully.";
    	}
    	
    	public String getTo()
    	{
    		return to;
    	}
    
    	public void setTo(String to)
    	{
    		this.to = to;
    	}
    
    	public String getBody()
    	{
    		return body;
    	}
    
    	public void setBody(String body)
    	{
    		this.body = body;
    	}
    
    	public static Properties getProperties()
    	{
    		return properties;
    	}
    
    	public static void setProperties(Properties properties)
    	{
    		Emailer.properties = properties;
    	}
    
    	public String getSubject()
    	{
    		return subject;
    	}
    
    	public void setSubject(String subject)
    	{
    		this.subject = subject;
    	}
    }
    .................................................. ..................................

    And if there are any technical reasons behind the scenes, let me know...please.
    Last edited by sagarsbodage; 01-15-2013 at 12:34 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Java mail API

    Please go through Guide For New Members and BB Code List - Java Programming Forum - Learn Java Programming then edit your post accordingly.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Java mail API

    I assume you are you sure your mail server actually supports mail without authentication?
    Also what exact error occurs then when you try? Post the complete error message.
    I like likes!

  4. #4
    sagarsbodage is offline sHUGAM
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Default Re: Java mail API

    Actually i am not getting any errors according to above code when i used to set authentication property but when i tried it with the following property: properties.put("mail.smtp.auth", "false"); and created a session object like this:- Session session = Session.getDefaultInstance(properties, null); i expect it should work but it's not....


    And as u said what errors occurs when i used it without authentication then following errors comes...


    javax.mail.AuthenticationFailedException
    Mail sending failed.
    at javax.mail.Service.connect(Service.java:306)
    at javax.mail.Service.connect(Service.java:156)
    at javax.mail.Service.connect(Service.java:105)
    at javax.mail.Transport.send0(Transport.java:168)
    at javax.mail.Transport.send(Transport.java:98)
    at com.email.Email.sendMail(Email.java:112)
    at com.email.Email.main(Email.java:66)

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Java mail API

    sagarsbodage, I asked you to edit your post. Were you unable to understand why, or are you just lazy?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. trying to sendin mail with java mail api
    By akhilsrivastava in forum Advanced Java
    Replies: 1
    Last Post: 04-04-2012, 09:34 PM
  2. send mail via java without java mail API
    By majidvadoostan in forum Networking
    Replies: 2
    Last Post: 04-26-2011, 09:30 PM
  3. Replies: 2
    Last Post: 07-25-2009, 09:36 AM
  4. Replies: 2
    Last Post: 06-04-2009, 01:42 PM
  5. Replies: 2
    Last Post: 05-05-2008, 06:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •