//part two as follows: public boolean isOK() {
return host != null && receiveAddress != null && fromAddress != null;
} public void sendMessage() {
Transport trans = null;
try {
if(receiveAddress == null) {
int toCount = receive.length;
receiveAddress = new InternetAddress[toCount];
for(int i = 0; i < toCount; i++) {
receiveAddress[i] = new InternetAddress(receive[i]);
}
}
if(fromAddress == null)
fromAddress = new InternetAddress(from); Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.ehlo", ehlo);
props.put("mail.smtp.auth", auth);
System.out.println("in try"); Session session = Session.getDefaultInstance(props, null); session.setDebug(debugFlag);
MimeMessage msg = new MimeMessage(session); msg.setFrom(fromAddress);
msg.setRecipients(javax.mail.Message.RecipientType.TO, receiveAddress); if(subject != null && !subject.equals(""))
msg.setSubject(subject, charset); if(replyTos != null || reply != null) {
if(reply != null) {
replyTos = new InternetAddress[reply.length];
for(int i = 0; i < reply.length; i++) {
replyTos[i] = new InternetAddress(reply[i]);
}
}
msg.setReplyTo(replyTos);
} msg.setSentDate(sendDate);
if(mimeParts != null && mimeParts.length != 0) {
Multipart mpart = new MimeMultipart();
if(content != null && !content.equals("")) {
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(content, contentType);
mpart.addBodyPart(mbp);
}
for(int i = 0; i < mimeParts.length; i++)
mpart.addBodyPart(mimeParts[i]);
msg.setContent(mpart);
} else if(attachmentFiles != null && attachmentFiles.length != 0) {
Multipart mpart = new MimeMultipart();
if(content != null && !content.equals("")) {
MimeBodyPart mb = new MimeBodyPart();
////mb.setContent(content, contentType);
mb.setText(content, charset);
mpart.addBodyPart(mb);
}
for(int i = 0; i < attachmentFiles.length; i++) {
MimeBodyPart mb = new MimeBodyPart();
FileDataSource fds = new FileDataSource(attachmentFiles[i]);
mb.setDataHandler(new DataHandler(fds));
mb.setFileName((new File(attachmentFiles[i])).getName());
mpart.addBodyPart(mb);
}
msg.setContent(mpart);
} else if(content != null && !content.equals(""))
msg.setContent(content, contentType);
msg.saveChanges();
trans = session.getTransport(receiveAddress[0]);
// register ourselves as listener for ConnectionEvents
// and TransportEvents
trans.addConnectionListener(this);
trans.addTransportListener(this);
if(username != null && password != null) {
trans.connect(host, username, password);
msg.saveChanges();
} else {
trans.connect();
}
//msg.saveChanges();
trans.sendMessage(msg, receiveAddress);
// give the EventQueue enough time to fire its events
try {Thread.sleep(5);}catch(InterruptedException e) {}
} catch (MessagingException mex) {
// give the EventQueue enough time to fire its events
try {Thread.sleep(5);}catch(InterruptedException e) {}
mex.printStackTrace();
System.out.println();
Exception ex = mex;
do {
if (ex instanceof SendFailedException) {
SendFailedException sfex = (SendFailedException)ex;
Address[] invalid = sfex.getInvalidAddresses();
if (invalid != null) {
System.out.println(" ** Invalid Addresses");
if (invalid != null) {
for (int i = 0; i < invalid.length; i++)
System.out.println(" " + invalid[i]);
}
}
Address[] validUnsent = sfex.getValidUnsentAddresses();
if (validUnsent != null) {
System.out.println(" ** ValidUnsent Addresses");
if (validUnsent != null) {
for (int i = 0; i < validUnsent.length; i++)
System.out.println(" "+validUnsent[i]);
}
}
Address[] validSent = sfex.getValidSentAddresses();
if (validSent != null) {
System.out.println(" ** ValidSent Addresses");
if (validSent != null) {
for (int i = 0; i < validSent.length; i++)
System.out.println(" "+validSent[i]);
}
}
}
System.out.println();
if (ex instanceof MessagingException)
ex = ((MessagingException)ex).getNextException();
else
ex = null;
} while (ex != null);
} finally {
try {
// close the transport
trans.close();
} catch (MessagingException mex) { /* ignore */ }
}
} // implement ConnectionListener interface
public void opened(ConnectionEvent e) {
System.out.println(">>> ConnectionListener.opened()");
}
public void disconnected(ConnectionEvent e) {}
public void closed(ConnectionEvent e) {
System.out.println(">>> ConnectionListener.closed()");
} // implement TransportListener interface
public void messageDelivered(TransportEvent e) {
System.out.print(">>> TransportListener.messageDelivered().");
System.out.println(" Valid Addresses:");
Address[] valid = e.getValidSentAddresses();
if (valid != null) {
for (int i = 0; i < valid.length; i++)
System.out.println(" " + valid[i]);
}
}
public void messageNotDelivered(TransportEvent e) {
System.out.print(">>> TransportListener.messageNotDelivered().");
System.out.println(" Invalid Addresses:");
Address[] invalid = e.getInvalidAddresses();
if (invalid != null) {
for (int i = 0; i < invalid.length; i++)
System.out.println(" " + invalid[i]);
}
}
public void messagePartiallyDelivered(TransportEvent e) {
// SMTPTransport doesn't partially deliver msgs
}
public static void main(String[] args) {
SMTPClient cl=new SMTPClient();
cl.setHost("smtp.etang.com"); cl.setFromAddress("[email protected]"); cl.setToAddresses(new String[]{"[email protected]", "[email protected]"} ); cl.setReplyToAddresses( new String[]{"[email protected]"} );
cl.enableAUTH();
cl.setUserName("youname");
cl.setPassword("youpassword");
cl.setCharset("gb2312"); cl.setSubject("测试");
cl.setFileAttachments(new String[]{"attachment1.txt", "k.jqs"});
cl.setTextContent("你好");
//cl.setContentType("text/html"); //for HTML messaging cl.sendMessage();
}
}
return host != null && receiveAddress != null && fromAddress != null;
} public void sendMessage() {
Transport trans = null;
try {
if(receiveAddress == null) {
int toCount = receive.length;
receiveAddress = new InternetAddress[toCount];
for(int i = 0; i < toCount; i++) {
receiveAddress[i] = new InternetAddress(receive[i]);
}
}
if(fromAddress == null)
fromAddress = new InternetAddress(from); Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.ehlo", ehlo);
props.put("mail.smtp.auth", auth);
System.out.println("in try"); Session session = Session.getDefaultInstance(props, null); session.setDebug(debugFlag);
MimeMessage msg = new MimeMessage(session); msg.setFrom(fromAddress);
msg.setRecipients(javax.mail.Message.RecipientType.TO, receiveAddress); if(subject != null && !subject.equals(""))
msg.setSubject(subject, charset); if(replyTos != null || reply != null) {
if(reply != null) {
replyTos = new InternetAddress[reply.length];
for(int i = 0; i < reply.length; i++) {
replyTos[i] = new InternetAddress(reply[i]);
}
}
msg.setReplyTo(replyTos);
} msg.setSentDate(sendDate);
if(mimeParts != null && mimeParts.length != 0) {
Multipart mpart = new MimeMultipart();
if(content != null && !content.equals("")) {
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(content, contentType);
mpart.addBodyPart(mbp);
}
for(int i = 0; i < mimeParts.length; i++)
mpart.addBodyPart(mimeParts[i]);
msg.setContent(mpart);
} else if(attachmentFiles != null && attachmentFiles.length != 0) {
Multipart mpart = new MimeMultipart();
if(content != null && !content.equals("")) {
MimeBodyPart mb = new MimeBodyPart();
////mb.setContent(content, contentType);
mb.setText(content, charset);
mpart.addBodyPart(mb);
}
for(int i = 0; i < attachmentFiles.length; i++) {
MimeBodyPart mb = new MimeBodyPart();
FileDataSource fds = new FileDataSource(attachmentFiles[i]);
mb.setDataHandler(new DataHandler(fds));
mb.setFileName((new File(attachmentFiles[i])).getName());
mpart.addBodyPart(mb);
}
msg.setContent(mpart);
} else if(content != null && !content.equals(""))
msg.setContent(content, contentType);
msg.saveChanges();
trans = session.getTransport(receiveAddress[0]);
// register ourselves as listener for ConnectionEvents
// and TransportEvents
trans.addConnectionListener(this);
trans.addTransportListener(this);
if(username != null && password != null) {
trans.connect(host, username, password);
msg.saveChanges();
} else {
trans.connect();
}
//msg.saveChanges();
trans.sendMessage(msg, receiveAddress);
// give the EventQueue enough time to fire its events
try {Thread.sleep(5);}catch(InterruptedException e) {}
} catch (MessagingException mex) {
// give the EventQueue enough time to fire its events
try {Thread.sleep(5);}catch(InterruptedException e) {}
mex.printStackTrace();
System.out.println();
Exception ex = mex;
do {
if (ex instanceof SendFailedException) {
SendFailedException sfex = (SendFailedException)ex;
Address[] invalid = sfex.getInvalidAddresses();
if (invalid != null) {
System.out.println(" ** Invalid Addresses");
if (invalid != null) {
for (int i = 0; i < invalid.length; i++)
System.out.println(" " + invalid[i]);
}
}
Address[] validUnsent = sfex.getValidUnsentAddresses();
if (validUnsent != null) {
System.out.println(" ** ValidUnsent Addresses");
if (validUnsent != null) {
for (int i = 0; i < validUnsent.length; i++)
System.out.println(" "+validUnsent[i]);
}
}
Address[] validSent = sfex.getValidSentAddresses();
if (validSent != null) {
System.out.println(" ** ValidSent Addresses");
if (validSent != null) {
for (int i = 0; i < validSent.length; i++)
System.out.println(" "+validSent[i]);
}
}
}
System.out.println();
if (ex instanceof MessagingException)
ex = ((MessagingException)ex).getNextException();
else
ex = null;
} while (ex != null);
} finally {
try {
// close the transport
trans.close();
} catch (MessagingException mex) { /* ignore */ }
}
} // implement ConnectionListener interface
public void opened(ConnectionEvent e) {
System.out.println(">>> ConnectionListener.opened()");
}
public void disconnected(ConnectionEvent e) {}
public void closed(ConnectionEvent e) {
System.out.println(">>> ConnectionListener.closed()");
} // implement TransportListener interface
public void messageDelivered(TransportEvent e) {
System.out.print(">>> TransportListener.messageDelivered().");
System.out.println(" Valid Addresses:");
Address[] valid = e.getValidSentAddresses();
if (valid != null) {
for (int i = 0; i < valid.length; i++)
System.out.println(" " + valid[i]);
}
}
public void messageNotDelivered(TransportEvent e) {
System.out.print(">>> TransportListener.messageNotDelivered().");
System.out.println(" Invalid Addresses:");
Address[] invalid = e.getInvalidAddresses();
if (invalid != null) {
for (int i = 0; i < invalid.length; i++)
System.out.println(" " + invalid[i]);
}
}
public void messagePartiallyDelivered(TransportEvent e) {
// SMTPTransport doesn't partially deliver msgs
}
public static void main(String[] args) {
SMTPClient cl=new SMTPClient();
cl.setHost("smtp.etang.com"); cl.setFromAddress("[email protected]"); cl.setToAddresses(new String[]{"[email protected]", "[email protected]"} ); cl.setReplyToAddresses( new String[]{"[email protected]"} );
cl.enableAUTH();
cl.setUserName("youname");
cl.setPassword("youpassword");
cl.setCharset("gb2312"); cl.setSubject("测试");
cl.setFileAttachments(new String[]{"attachment1.txt", "k.jqs"});
cl.setTextContent("你好");
//cl.setContentType("text/html"); //for HTML messaging cl.sendMessage();
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货