here is my code import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*;
public class sendMail { public static void main (String args[]) throws Exception { String host = "smtp.gmail.com"; String from = "mravikrish@gmail.com"; String to = "mravikrish@yahoo.com"; String fileAttachment = "hello"; Properties props = System.getProperties(); props.put("mail.smtp.host", host); Session session = Session.getInstance(props, null); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO,new InternetAddress(to)); message.setSubject("Hello JavaMail Attachment"); MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText("Hi"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); messageBodyPart = new MimeBodyPart(); DataSource source =new FileDataSource(fileAttachment); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(fileAttachment); multipart.addBodyPart(messageBodyPart); message.setContent(multipart); Transport.send( message ); } }
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35256
7
posted
0
Not sure what's wrong with the code, but you will definitely need to add authentication code (i.e., a password handler). No remote email server will let you connect these days without that.
public void send(HttpServletRequest req) { try { String host = "smtp.mail.com"; String from = "mymail@gmail.com"; String to = "mymail@yahoo.com"; System.out.println("Host ="+host); Properties props = System.getProperties(); props.put("mail.smtp.host", host); props.put("mail.smtp.auth", "true"); JAuthenticate pAuth = new JAuthenticate("mymail@gmail.com", "mypwd"); Session session = Session.getInstance(props, pAuth); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject(" Multiple Attachments "); MimeBodyPart messageBodyPart = new MimeBodyPart(); String mssg="Multiple Attachments Test Mail"; messageBodyPart.setText(mssg); System.out.println(" Before senidng message to mail"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); message.setContent(multipart); Transport.send( message ); System.out.println(" After senidng message to mail");
} catch(Exception e) { e.printStackTrace(); } } }
for this code iam getting the following exception
javax.mail.MessagingException: Could not connect to SMTP host: smtp.mail.com, port: 25; nested exception is: java.net.ConnectException: Connection timed out: connect at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1213) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:311) at javax.mail.Service.connect(Service.java:255) at javax.mail.Service.connect(Service.java:134) at javax.mail.Service.connect(Service.java:86) at com.sun.mail.smtp.SMTPTransport.connect(SMTPTransport.java:144) at javax.mail.Transport.send0(Transport.java:150) at javax.mail.Transport.send(Transport.java:80) at com.esd.Login.sendMail1.send(sendMail1.java:35) at com.esd.Login.ServletMail.doGet(ServletMail.java:25) at javax.servlet.http.HttpServlet.service(HttpServlet.java:690) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447) at java.lang.Thread.run(Thread.java:595)
neelima neelima wrote:hi
i had run this program but got the following exception
javax.mail.MessagingException: Unknown SMTP host: smtp.gmail.com;
nested exception is:
java.net.UnknownHostException: smtp.gmail.com
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1543)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:453)
at javax.mail.Service.connect(Service.java:313)
at javax.mail.Service.connect(Service.java:172)
at javax.mail.Service.connect(Service.java:121)
at javax.mail.Transport.send0(Transport.java:190)
at javax.mail.Transport.send(Transport.java:120)
at mail.Msg.<init>(Msg.java:47)
at mail.Msg.main(Msg.java:57)
Caused by: java.net.UnknownHostException: smtp.gmail.com
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:550)
at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:141)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:267)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:201)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1511)
... 8 more