3
回答
JavaMail程序如何通过代理连接邮件服务器收发邮件?
注册华为云得mate10,2.9折抢先购!>>>   

import java.sql.Timestamp;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class TestMail ...{

   
/** *//**
     *
@param args
    
*/

   
public static void main(String[] args) ...{

        String smtphost
= "smtp.163.com"; // 发送邮件服务器
        String user = "fengguang1982"; // 邮件服务器登录用户名
        String password = "XXXXX"; // 邮件服务器登录密码
        String from = "fengguang1982@163.com"; // 发送人邮件地址
        String to = "fengguang@icss.com.cn"; // 接受人邮件地址
        String subject = "javamail测试"
               
+ (new Timestamp(System.currentTimeMillis())); // 邮件标题
        String body = "Java Mail Body!"; // 邮件内容
        Properties props = new Properties();
        props.put(
"mail.smtp.host", smtphost);
        props.put(
"mail.smtp.auth", "true");
        Session ssn
= Session.getInstance(props, null);

        MimeMessage message
= new MimeMessage(ssn);

       
try ...{
            InternetAddress fromAddress
= new InternetAddress(from);
            message.setFrom(fromAddress);
            InternetAddress toAddress
= new InternetAddress(to);
            message.addRecipient(Message.RecipientType.TO, toAddress);

            message.setSubject(subject);
            message.setText(body);

            Transport transport
= ssn.getTransport("smtp");
            transport.connect(smtphost, user, password);
            transport.sendMessage(message, message
                    .getRecipients(Message.RecipientType.TO));
           
//transport.send(message);
            transport.close();
            System.out.println(
"mail has been send");
        }
catch (MessagingException e) ...{
           
// TODO Auto-generated catch block
            e.printStackTrace();
        }


    }


}

鄙人参照上述代码写了一个JavaMail发送邮件的测试程序,该程序在不需要代理能直接上网的机器上运行正常(邮件能发送出去),在需要代理上网的机器上运行失败,失败原因是连接不上邮件服务器。问题困扰本人好几天了,还望各位大侠指点迷津,先在此谢过了。

举报
小元宵
发帖于5年前 3回/1K+阅
顶部