Results 1 to 5 of 5

Thread: Hi Everyone

  1. #1
    Join Date
    Aug 2013
    Posts
    4
    Rep Power
    0

    Question Hi Everyone

    Can you guys please help me in how to write code for sending mail through network proxy with out any exceptions and I've seen so many examples but none worked.

    Thanx in Advance.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,002
    Rep Power
    3

    Default Re: Hi Everyone

    If nothing worked, then you must have a non-code problem. The internets could be down, mailserver down, proxy down. Could be lots of things. If you are sure it's the code, then "doesn't work" is not good enough. You could provide the code, error codes, stacktraces, screenshots, logfiles, you name it.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: Hi Everyone

    Moved out of admin only section.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    Join Date
    Aug 2013
    Posts
    4
    Rep Power
    0

    Default Re: Hi Everyone

    Hi Surfman,
    Here is the code it is working fine with normal internet but tried with network proxy no success and I'm getting exception like javax.mail.MessagingException: Could not connect to SMTP host, java.net.ConnectException: Connection refused. Is there any other way to write code for network proxy to send email as localhost please help me Thanx.


    public class mail {
    private static void main (string[] args) {

    String[] to = {"***", "****"};

    String host = "smtp.gmail.com";
    String from = "****";
    String pass = "****";

    Properties properties = System.getProperties();

    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.smtp.host", host);
    properties.put("mail.smtp.user", from);
    properties.put("mail.smtp.password", pass);
    properties.put("mail.smtp.port", "587");//587,465,25
    properties.put("mail.smtp.auth", "true");
    // properties.put("mail.smtp.debug", "true");

    Session session = Session.getDefaultInstance(properties, null);
    session.setDebug(true);

    try {

    MimeMessage message = new MimeMessage(session);

    message.setFrom(new InternetAddress(from));

    InternetAddress[] addressTo = new InternetAddress[to.length];
    for (int i = 0; i < to.length; i++) {
    addressTo[i] = new InternetAddress(to[i]);
    }

    message.setRecipients(Message.RecipientType.TO, addressTo);

    message.setSubject("Hello");

    BodyPart messageBodyPart = new MimeBodyPart();

    messageBodyPart.setText("Hi All, \n\n"
    + "Please see the link bellow"
    + "\n\nKind regards,"
    + "\n Sandy");

    Multipart multipart = new MimeMultipart();

    multipart.addBodyPart(messageBodyPart);

    messageBodyPart = new MimeBodyPart();
    String filename = prop.getProperty("path") + FILENAME;
    DataSource source = new FileDataSource(filename);
    messageBodyPart.setDataHandler(new DataHandler(source));
    messageBodyPart.setFileName(filename);
    multipart.addBodyPart(messageBodyPart);

    message.setContent(multipart);
    // Send message
    Transport transport = session.getTransport("smtp");
    transport.connect(host, from, pass);
    transport.sendMessage(message, message.getAllRecipients());
    transport.close();
    logger.info("Sent message successfully....");

    } catch (MessagingException e) {
    logger.error(e);
    }
    }

  5. #5
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,152
    Rep Power
    6

    Default Re: Hi Everyone

    Unsuprisingly the JavaMail FAQ has something to say about connecting through a proxy:

    JavaMail API - FAQ
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Posting Permissions

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