Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

sending an email in java

  • 26-03-2007 12:53pm
    #1
    Registered Users, Registered Users 2 Posts: 1,305 ✭✭✭


    hi i'm trying to send an email but i'm having a bit of trouble. the code below is what i'm using. there's no errors but the email doesnt send! just wondering if anyone could help! thanks

    import java.util.Date;
    import java.util.Properties;
    import javax.mail.Authenticator;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.AddressException;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    
    import java.io.UnsupportedEncodingException;
    
    public class SendEmail {
    	
    	public void sendMail()
    	{
    		Properties myProperties=new Properties();
                    
    		
    		myProperties.put("mail.smtp.host", "smtp.gawab.com");
    		myProperties.put("mail.smtp.auth", "true");
                    
    		Authenticator sc= new ServerConnection();
    		Session ses = Session.getDefaultInstance(myProperties,sc);
    	
    		Message msg=new MimeMessage(ses);
    		
    		try {
    			msg.setFrom(new InternetAddress("me@gawab.com","name"));
    			
    			msg.setRecipient(Message.RecipientType.TO,new InternetAddress("address@gmail.com"));
    
    			msg.setSubject("TEST");
    			String body = "Test Mail";
    			msg.setText(body);
    			
    			msg.setSentDate(new Date());
    			
    			Transport.send(msg);
    			
    		} catch (AddressException e) {
    			e.printStackTrace();
    		} catch (MessagingException e) {
    			e.printStackTrace();
    		} catch (UnsupportedEncodingException e) {
    			e.printStackTrace();
    		}
    	}
            
            
    
    }
    

    the code below is how i'm passing in my username and passsword to the host!
    import javax.mail.PasswordAuthentication;
    
    public class ServerConnection extends javax.mail.Authenticator
    {
     public PasswordAuthentication ServerLogin()
        {
            String username = "name";
            String password = "password";
            return new PasswordAuthentication(username, password);
        }
    }
    


Comments

  • Closed Accounts Posts: 7,230 ✭✭✭scojones


    Do you have a mail server running on the computer that this code is executed on?


  • Registered Users, Registered Users 2 Posts: 4,188 ✭✭✭pH


    You don't need a mail server running on the machine that's running the java code, I've used javax.mail from clients to SMTP servers without problem.

    However, SMTP by default doesn't use sender identification and many live mail servers (because of spam/relay etc) are very picky about how clients connect and what from/to addresses they use.

    You code looks very similar to mine (that works and does exactly the same thing without authentication) so possibly it's down to the mail server.

    One other thing - don't suppress the exceptions by printing them, throw them on as runtime exceptions just in case your code is throwing an exception and your not capturing the console output properly.


  • Registered Users, Registered Users 2 Posts: 610 ✭✭✭nialo


    Either that or add in a catch block for all exceptions.
    catch( Exception e)
    {
    }
    


  • Registered Users, Registered Users 2 Posts: 2,013 ✭✭✭lynchie


    myProperties.put("mail.debug", "true");
    

    and retry


  • Registered Users, Registered Users 2 Posts: 1,305 ✭✭✭jobonar


    thanks for the replies

    lynchie wrote:
    myProperties.put("mail.debug", "true");
    

    and retry

    do i add that line in or do i replace a line with that code?


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,987 ✭✭✭Ziycon


    If your running it on a college or work network the smtp port may be shutdown which would stop the mail being sent and wound'nt give any errors.


  • Registered Users, Registered Users 2 Posts: 1,305 ✭✭✭jobonar


    Ziycon wrote:
    If your running it on a college or work network the smtp port may be shutdown which would stop the mail being sent and wound'nt give any errors.

    no i'm running it on my laptop at home!


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    nialo wrote:
    Either that or add in a catch block for all exceptions.
    catch( Exception e)
    {
    }
    

    That wouldn't do anything. If there was an uncaught error it would just stop the program. Looks like all their exceptions that would be thrown are being caught.

    Do you have a tool for intercepting the traffic of whats going to the port? Could try looking at that.

    Also trying using putty and connect to the SMTP port and manually type a test mail in. See if it refuses you for some reason.


  • Registered Users, Registered Users 2 Posts: 1,127 ✭✭✭smcelhinney


    you're not on one of these weird ISPs that proxys requests and requires authentication are you?

    Try setting a proxy, cant remember the syntax but if you google 'http.proxy java' that should get you somewhere. Its something like
    System.setProperties("http_proxy.user", username);
    System.setProperties("http_proxy.pass", password);
    

    NB: Be careful using System.setProperties though, these variables are then set globally. You might consider trying this in the command line first

    eg
    java -Dhttp.proxyHost=your.proxy.dom -Dhttp.proxyPort=XX -Dhttp.proxyUser=foo -Dhttp.proxyPass=bar SendEmail
    

    HTH,
    Stephen


  • Registered Users, Registered Users 2 Posts: 2,013 ✭✭✭lynchie


    jobonar wrote:
    thanks for the replies




    do i add that line in or do i replace a line with that code?

    You add it in.. though if you had to ask that, you should probably read up some more on the javamail API.

    Turn on debug and you will see the full smtp conversation, and then you can figure out if its a proxy issue] / auth issue / connection issue.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 500 ✭✭✭warrenaldo


    here is my code for the exact same problem. very similar to yours - using javamail.

    use mine as compare and contrast - it may help
    lynchie wrote:
    public void postMail(String recipient, String subject, String message) throws MessagingException
    {
    String from = "fromemail@google.com";
    //String finalmessage = "FOR YOUR ATTENTION\n\nA Email has been assigned to you!!!\n\n"
    // + message +
    // "\nThanks and Regards" +
    // "\nMy Deadly System.";
    boolean debug = false;
    //Set the host smtp address
    Properties props = new Properties();
    props.put("mail.smtp.host", "mysmtphost");
    props.put("mail.smtp.auth", "true");

    // create some properties and get the default Session
    Authenticator loAuthenticator = new MailAuthenticator();
    Session session = Session.getDefaultInstance(props, loAuthenticator);
    session.setDebug(debug);

    // create a message
    Message msg = new MimeMessage(session);

    // set the from and to address
    InternetAddress addressFrom = new InternetAddress(from);
    msg.setFrom(addressFrom);

    InternetAddress[] addressTo = new InternetAddress[1];
    addressTo[0] = new InternetAddress(recipient);
    msg.setRecipients(Message.RecipientType.TO, addressTo);

    // Setting the Subject and Content Type
    msg.setSubject(subject);
    msg.setContent(message, "text/plain");
    System.out.println("Sending");
    Transport.send(msg);
    }

    public class MailAuthenticator extends Authenticator {

    public PasswordAuthentication
    getPasswordAuthentication() {
    String username, password;

    username = "myusername";
    password = "mypassword";

    return new PasswordAuthentication(
    username, password);
    }
    }*/

    Without the password authentication i cpould not send external mail - only internal.
    if you want to check this out maybe sending mail to yourself is the best bet. at least that way its going to your smtp host and no1 elses. it might simulate internal mail only - not sure.


Advertisement