java - Javamail, error when sending attachments. -
i trying send email. when send without attachment, sends email correctly. if try attach something, doesn't work.
the class:
import com.sun.mail.smtp.smtptransport; import java.io.file; import java.security.security; import java.util.date; import java.util.properties; import javax.activation.datahandler; import javax.activation.filedatasource; import javax.mail.bodypart; import javax.mail.message; import javax.mail.messagingexception; import javax.mail.multipart; import javax.mail.part; import javax.mail.session; import javax.mail.transport; import javax.mail.internet.addressexception; import javax.mail.internet.internetaddress; import javax.mail.internet.mimebodypart; import javax.mail.internet.mimemessage; import javax.mail.internet.mimemultipart; import javax.swing.joptionpane; /** * * @author doraemon */ public class googlemail { public static void send(string from, string pass, string[] to, string assunto, string mensagem, file[] anexos) { string host = "smtp.risantaisabel.com.br"; properties props = system.getproperties(); props.put("mail.smtp.starttls.enable", "true"); // added line props.put("mail.smtp.host", host); props.put("mail.smtp.user", from); props.put("mail.smtp.password", pass); props.put("mail.smtp.port", "587"); props.put("mail.smtp.auth", "true"); //string[] = {"tarcisioambrosio@gmail.com"}; // added line session session = session.getdefaultinstance(props, null); mimemessage message = new mimemessage(session); try { message.setfrom(new internetaddress(from)); } catch (addressexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (messagingexception e) { // todo auto-generated catch block e.printstacktrace(); } internetaddress[] toaddress = new internetaddress[to.length]; boolean enviado = true; // array of addresses for( int i=0; < to.length; i++ ) { // changed while loop try { toaddress[i] = new internetaddress(to[i]); } catch (addressexception e) { // todo auto-generated catch block e.printstacktrace(); } } // system.out.println(message.recipienttype.to); for( int i=0; < toaddress.length; i++) { // changed while loop try { message.addrecipient(message.recipienttype.to, toaddress[i]); } catch (messagingexception e) { // todo auto-generated catch block e.printstacktrace(); } } try { message.setsubject(assunto); //message.setcontent(mensagem, "text/plain"); message.settext(mensagem); transport transport = session.gettransport("smtp"); transport.connect(host, from, pass); if(anexos.length == 0){ } else { multipart mp = new mimemultipart(); bodypart messagebodypart = new mimebodypart(); for(int = 0; < anexos.length;i++) { mimebodypart mbp2 = new mimebodypart(); filedatasource fds = new filedatasource(anexos[i].getpath()); mbp2.setdatahandler(new datahandler(fds)); mbp2.setfilename(fds.getname()); mp.addbodypart(mbp2); } messagebodypart.setcontent(message, "multipart/mixed"); mp.addbodypart(messagebodypart); message.setcontent(mp); } transport.sendmessage(message, message.getallrecipients()); transport.close(); } catch (messagingexception e) { // todo auto-generated catch block joptionpane.showmessagedialog(null, "<html><b>email não enviado, tente novamente confirmando seus dados"); enviado = false; e.printstacktrace(); } if(enviado) { joptionpane.showmessagedialog(null, "<html><b>email enviado.</html></b> \n\n caso tenha digitado errado o email, somente pela sua caixa de entrada poderá confirmar se chegou. \n\n<html> email da parte digitado:<b><font size = 3 color=#ff0000> " + to[0]); } } }
if change the setcontent text/plain, get:
javax.mail.messagingexception: ioexception while sending message; nested exception is: java.io.ioexception: "text/html" datacontenthandler requires string object, given object of type class javax.mail.internet.mimemessage @ com.sun.mail.smtp.smtptransport.sendmessage(smtptransport.java:1167)
if change setcontent multipart/mixed, get:
javax.mail.messagingexception: mime part of type "multipart/mixed" contains object of type javax.mail.internet.mimemessage instead of mimemultipart
do have idea how can fix this? thanks.
remove
message.settext(mensagem);
change
messagebodypart.setcontent(message, "multipart/mixed");
to
messagebodypart.settext(mensagem);
and move , following line above "for" loop.
also, see this javamail faq entry of common mistakes.
Comments
Post a Comment