现在我用一个qq企业邮箱,向客户发送邮件的时候,默认的邮箱123456.cn,现在我想动态的更改发送人,使到客户收到邮件后,可以直接向我所提供的发信人联系。
如向客户[email protected]发送,我把发送人设置为[email protected],用户收到邮件后发信人就不是我的123456.cn,而是[email protected],要这种效果。
代码是spring做的,大家帮帮忙~package com.cc;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;import javax.mail.internet.MimeMessage;
import javax.mail.MessagingException;
public class TestEmail {
public static JavaMailSender sender;
public static void main(String[] args) throws Exception {
ApplicationContext ctx = new FileSystemXmlApplicationContext(
"src/applicationContext.xml");
sender = (JavaMailSender) ctx.getBean("mailSender");
//测试发送只有文本信息的简单测试
sendText("[email protected]","邮件~~~~~~~~~~~","收到了没~收到回下~");
}
public static void sendText(String recipient, String subject, String content)
throws MessagingException
{
MimeMessage mimeMsg = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMsg, true, "UTF-8");
helper.setTo(recipient);
helper.setFrom("[email protected]");
helper.setSubject(subject);
helper.setText(content);
sender.send(mimeMsg);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="mailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.exmail.qq.com" />
<property name="username" value="123456.cn" />
<property name="password" value="123456" />
<property name="port" value="465" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">60000</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.transport.protocol">smtp</prop>
<prop key="mail.smtp.socketFactory.port">465</prop>
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
<prop key="mail.smtp.socketFactory.fallback">false</prop>
</props>
</property>
</bean>
</beans>
如向客户[email protected]发送,我把发送人设置为[email protected],用户收到邮件后发信人就不是我的123456.cn,而是[email protected],要这种效果。
代码是spring做的,大家帮帮忙~package com.cc;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;import javax.mail.internet.MimeMessage;
import javax.mail.MessagingException;
public class TestEmail {
public static JavaMailSender sender;
public static void main(String[] args) throws Exception {
ApplicationContext ctx = new FileSystemXmlApplicationContext(
"src/applicationContext.xml");
sender = (JavaMailSender) ctx.getBean("mailSender");
//测试发送只有文本信息的简单测试
sendText("[email protected]","邮件~~~~~~~~~~~","收到了没~收到回下~");
}
public static void sendText(String recipient, String subject, String content)
throws MessagingException
{
MimeMessage mimeMsg = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMsg, true, "UTF-8");
helper.setTo(recipient);
helper.setFrom("[email protected]");
helper.setSubject(subject);
helper.setText(content);
sender.send(mimeMsg);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="mailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.exmail.qq.com" />
<property name="username" value="123456.cn" />
<property name="password" value="123456" />
<property name="port" value="465" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">60000</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.transport.protocol">smtp</prop>
<prop key="mail.smtp.socketFactory.port">465</prop>
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
<prop key="mail.smtp.socketFactory.fallback">false</prop>
</props>
</property>
</bean>
</beans>
Failed message 1:
com.sun.mail.smtp.SMTPSendFailedException: 501 mail from address must be same as authorization user at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:403)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:342)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:338)
at com.cc.TestEmail.sendText(TestEmail.java:42)
at com.cc.TestEmail.main(TestEmail.java:29)
总是报这个异常
我这里有个例子你参考一下,或许对你有用
1.java文件
package com.soft.mail;import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;public class MailService {
private MailSender mailSender;
private SimpleMailMessage mailMessage;//邮件模板
public void sendMail() {
System.out.println("-----------发送邮件!---------");
SimpleMailMessage msg=new SimpleMailMessage(this.mailMessage);
msg.setText("this is a test mail");
try {
mailSender.send(msg);
} catch (MailException e) {
e.printStackTrace();
}
System.out.println("-----------发送成功!---------");
}
public MailSender getMailSender() {
return mailSender;
}
public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
}
public SimpleMailMessage getMailMessage() {
return mailMessage;
}
public void setMailMessage(SimpleMailMessage mailMessage) {
this.mailMessage = mailMessage;
}
}
2.配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="mailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.163.com"/>
<!-- 有些邮件服务器发送邮件需要认证,所以必须提供帐号 -->
<property name="username" value="[email protected]"/>
<property name="password" value=""/>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
</props>
</property>
</bean>
<bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="to" value="[email protected]"></property>
<property name="from" value="[email protected]"></property>
<property name="subject" value="邮件测试"></property>
</bean>
<bean id="ms" class="com.soft.mail.MailService">
<property name="mailSender" ref="mailSender"></property>
<property name="mailMessage" ref="mailMessage"></property>
</bean>
<bean id="reportJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="ms"></property>
<property name="targetMethod" value="sendMail"></property>
</bean>
<!-- 一个简单的调度器 -->
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="reportJob"></property>
<property name="startDelay" value="1"></property>
<property name="repeatInterval" value="1000"/>
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="simpleTrigger"/>
</list>
</property>
</bean>
</beans>
邮件的发送人,必须和权限验证的用户名一样。
<prop key="mail.smtp.auth">true</prop>除此以外就只有改安全机制逻辑了,SMTPTransport.java
<prop key="mail.smtp.auth">true</prop>除此以外就只有改安全机制逻辑了,SMTPTransport.java
<prop key="mail.smtp.auth">true</prop>除此以外就只有改安全机制逻辑了,SMTPTransport.java
除非它本身就提供了某種調節機制