发送邮件:
package com.demo.common.util; import java.util.Date; import java.util.Properties; import javax.mail.Address; import javax.mail.Authenticator; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; 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 org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class MailClient { public void sendMail(String mailServer, String from, String to, String subject, String messageBody) throws MessagingException, AddressException { // Setup mail server Properties props = System.getProperties(); props.put("mail.smtp.host", mailServer); // Get a mail session Session session = Session.getDefaultInstance(props, null); // Define a new mail message Message message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject(subject); // Create a message part to represent the body text BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText(messageBody); // use a MimeMultipart as we need to handle the file attachments Multipart multipart = new MimeMultipart(); // add the message body to the mime message multipart.addBodyPart(messageBodyPart); // Put all message parts in the message message.setContent(multipart); // Send the message Transport.send(message); } public static void main(String[] args) { try { MailClient client = new MailClient(); String server="mail.gmail.com"; String from="johndoe@gmail.com"; String to = "johndoe@gmail.com"; String subject="Test"; String message="Testing"; client.sendMail(server,from,to,subject,message); } catch(Exception e) { e.printStackTrace(System.out); } } }
如果你用myEclipse进行开发的话,运行时可能会出现以下的Exception错误:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream at javax.mail.Session.loadProvidersFromStream(Session.java:928) at javax.mail.Session.access$000(Session.java:174) at javax.mail.Session$1.load(Session.java:870) at javax.mail.Session.loadResource(Session.java:1084) at javax.mail.Session.loadProviders(Session.java:889) at javax.mail.Session.<init>(Session.java:210) at javax.mail.Session.getDefaultInstance(Session.java:299) at javax.mail.Session.getDefaultInstance(Session.java:339) at com.happy.mail.SendMail.send(SendMail.java:58) at com.happy.mail.SendMail.main(SendMail.java:37)
原因是jar包版本不统一,解决方法如下:
删除MyEclipse下JavaEE Libraries/javaee.jar/mail里的包有东西.导入mail.jar
MyEclipse6.6具体方法如下:
用rar打开D:\Program Files\MyEclipse 6.6\myeclipse\eclipse\plugins\com.genuitec.eclipse.j2eedt.core_6.6.0.zmyeclipse660200810\data\libraryset\EE_5/javaee.jar,删除其中的mail目录
MyEclipse8.5具体方法如下:
用rar打开D:\Program Files\MyEclipse 6.6\myeclipse\eclipse\plugins \com.genuitec.eclipse.j2eedt.core_8.5.0.me201003231033\data\libraryset \EE_5\javaee.jar,删除其中的mail目录
相关推荐
JavaMail的java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream错误 原因: MyEclipse6.5的javaee.jar中的mail包与JavaMail包有冲突。 解决: 在MyEclipse目录下(D:\Program Files\MyEclipse ...
java.lang.NoClassDefFoundError: com/sun/activation/registries/LogSupport异常处理
java.lang.NoClassDefFoundError: de/javakaffee/kryoserializers/CurrencySerializer
该jar包解决报错java.lang.NoClassDefFoundError: org/apache/james/mime4j/MimeException,亲测可用。
在Java开发过程中,我们经常会遇到`java.lang.NoClassDefFoundError`这个异常,尤其是在进行JDK版本升级时。这个错误通常表示在运行时找不到某个类的定义,即使编译时该类是可用的。在本例中,问题发生在从一个较低...
标题 "java.lang.NoClassDefFoundError: javax/persistence/EntityListener" 提到的问题是一个常见的Java运行时异常,通常表示在类加载时找不到指定的类定义。这个错误在Java应用程序或Web应用中出现,可能是因为...
标题中的“JSON中,java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher问题解决”指的是一类常见的Java运行时错误,当尝试加载一个类时,如果Java虚拟机(JVM)找不到该类的定义,就会抛出`...
在Java编程中,`java.lang.NoClassDefFoundError` 是一个常见的运行时错误,它发生在类加载器尝试执行一个类,但在类路径中找不到该类的定义时。在这个特定的场景中,问题聚焦于 `net.sf.ezmorph.Morpher` 类。`...
jackson-annotations-2.10.2.jar jackson-core-2.10.2.jar jackson-databind-2.10.2.jar NoClassDefFoundError解决
在Java编程中,`java.lang.NoClassDefFoundError` 是一个常见的运行时异常,它发生在类加载器尝试加载一个在编译时存在但在运行时找不到的类定义时。这个错误通常意味着类路径设置不正确或者依赖项没有正确地被包含...
flink-hadoop-compatibility_2.12-1.7.1.jar javax.ws.rs-api-2.0.1.jar jersey-common-2.27.jar ...Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/jersey/api/client/config/ClientConfig
NULL 博文链接:https://jaychang.iteye.com/blog/980159
jdk升级jdk10后,原本jdk自带的 webservice一些包确实,引起的一系列错误解决方案
在Java编程中,遇到“Exception in thread 'main' java.lang.NoClassDefFoundError”是一种常见的异常情况,这通常意味着JVM在运行时未能找到指定的类定义。此错误不同于ClassNotFoundException,后者发生在尝试加载...
Java编程中的`java.lang.NoClassDefFoundError: org/jboss/logging/`是一个常见的运行时错误,通常发生在尝试执行一个类时,JVM无法找到在编译时已经存在的类定义。这个错误并不意味着类在编译期间不存在,而是表明...
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/jersey/api/client/config/ClientConfig at org.apache.hadoop.yarn.client.api.TimelineClient.createTimelineClient(TimelineClient.java:...
该开发包是解决 JavaMail 发送邮件报错:java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream错误 ava.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream 使用方法: 将javaee.jar ...
打到org/eclipse/ui/internal/util/SWTResourceUtil.class 用winrar打开,同时也打开3.4的org.eclipse.ui.workbench_3.4.0.I20080606-1300.jar 到org/eclipse/ui/internal/util目录下 把SWTResourceUtil.class ...
(1)java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream (2)java.lang.NoClassDefFoundError: com/sun/activation/registries/LogSupport 的解决办法,还有对应的两个包:mail.jar和activation.jar ...