Javamail smtp port number

Discussion in 'Java' started by autogoor@yahoo.com, Feb 12, 2005.

  1. Guest

    I use the following code to send email. I do not know to use the
    default 25 for smtp port. How can I set a different number?

    public static void sendMail(
    String smtpServer,
    String to,
    String cc,
    String from,
    String subject,
    String body){


    try{


    Properties props = System.getProperties();
    props.put("mail.smtp.host", smtpServer);
    Session session = Session.getDefaultInstance(props, null);


    Message msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(from));
    msg.setRecipients(Message.RecipientType.TO,


    InternetAddress.parse(to, false));


    if (cc != null){
    msg.setRecipients(Message.RecipientType.CC,
    InternetAddress.parse(cc, false));
    }


    msg.setSubject(subject);
    msg.setText(body);
    msg.setHeader("X-Mailer", "LOTONtechEmail");
    msg.setSentDate(new Date());
    Transport.send(msg);


    System.out.println("Mail sent OK");
    }catch (Exception ex){
    ex.printStackTrace();
    }
    }
     
    , Feb 12, 2005
    #1
    1. Advertising

  2. Roland Guest

    On 12-2-2005 21:15, wrote:

    > I use the following code to send email. I do not know to use the
    > default 25 for smtp port. How can I set a different number?
    >
    > public static void sendMail(
    > String smtpServer,
    > String to,
    > String cc,
    > String from,
    > String subject,
    > String body){
    >
    >
    > try{
    >
    >
    > Properties props = System.getProperties();
    > props.put("mail.smtp.host", smtpServer);

    props.put("mail.smtp.port", Integer.toString(888));
    > Session session = Session.getDefaultInstance(props, null);
    >
    >
    > Message msg = new MimeMessage(session);
    > msg.setFrom(new InternetAddress(from));
    > msg.setRecipients(Message.RecipientType.TO,
    >
    >
    > InternetAddress.parse(to, false));
    >
    >
    > if (cc != null){
    > msg.setRecipients(Message.RecipientType.CC,
    > InternetAddress.parse(cc, false));
    > }
    >
    >
    > msg.setSubject(subject);
    > msg.setText(body);
    > msg.setHeader("X-Mailer", "LOTONtechEmail");
    > msg.setSentDate(new Date());
    > Transport.send(msg);
    >
    >
    > System.out.println("Mail sent OK");
    > }catch (Exception ex){
    > ex.printStackTrace();
    > }
    > }
    >

    --
    Regards,

    Roland de Ruiter
    ___ ___
    /__/ w_/ /__/
    / \ /_/ / \
     
    Roland, Feb 12, 2005
    #2
    1. Advertising

  3. Guest

    Thank you so much. Now the port part worked. But I still got the
    following message. I think I need to gave username and password for my
    1and1 account. Could you tell me how to set username and password?

    THanks, again
    ggor

    javax.mail.SendFailedException: Invalid Addresses;
    nested exception is:
    class com.sun.mail.smtp.SMTPAddressFailedException: 550 won't deliver
    to <>

    at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1130)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:525)
    at javax.mail.Transport.send0(Transport.java:151)
    at javax.mail.Transport.send(Transport.java:80)
     
    , Feb 13, 2005
    #3
  4. <> wrote in message
    news:...
    > Thank you so much. Now the port part worked. But I still
    > got the following message. I think I need to gave username
    > and password for my 1and1 account. Could you tell me how
    > to set username and password?
    >


    First, ensure that you do this:

    ...
    props.put("mail.smtp.auth", true);
    ...

    Then, change your approach slightly - instead of this:

    Transport.send(msg);

    do something like:

    ...
    msg.saveChanges();
    ...
    // connect to the transport
    Transport transport = session.getTransport("smtp");
    transport.connect(smtpHost, " ", " "); // host, user, password

    // send the msg and close the connection
    transport.sendMessage(msg, msg.getAllRecipients());
    transport.close();
    ...

    Once again, check the FAQ - I previously posted the link.

    I hope this helps.

    Anthony Borla
     
    Anthony Borla, Feb 13, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joseph Millar

    Re: Smtp Server error with JavaMail

    Joseph Millar, Jul 16, 2003, in forum: Java
    Replies:
    0
    Views:
    657
    Joseph Millar
    Jul 16, 2003
  2. GaryM
    Replies:
    8
    Views:
    19,908
    Scott Yanoff
    Jul 21, 2003
  3. Sudsy
    Replies:
    0
    Views:
    482
    Sudsy
    Jul 16, 2003
  4. mrby
    Replies:
    6
    Views:
    929
    GaryM
    May 6, 2004
  5. Blacksheep

    JAVAMAIL SMTP error 421

    Blacksheep, Jul 9, 2004, in forum: Java
    Replies:
    3
    Views:
    10,194
    GaryM
    Aug 1, 2004
Loading...

Share This Page