提交 85a8d128 编写于 作者: R redsolo

Added a "Test configuration to send email" feature to the global configuration of email settings.


git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@6133 71c3de6d-444a-0410-be80-ed276b4c234a
上级 8403cc77
package hudson.tasks;
import hudson.Launcher;
import hudson.Proc;
import hudson.model.Build;
import hudson.model.BuildListener;
import hudson.model.Descriptor;
import hudson.model.Hudson;
import hudson.model.Project;
import hudson.model.TaskListener;
import hudson.model.User;
import hudson.model.UserPropertyDescriptor;
import hudson.util.ByteBuffer;
import hudson.util.FormFieldValidator;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.StringWriter;
import java.util.Date;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
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.MimeMessage;
import javax.servlet.ServletException;
import org.apache.tools.ant.types.selectors.SelectorUtils;
......@@ -135,6 +151,11 @@ public class Mailer extends Publisher {
* If true use SSL on port 465 (standard SMTPS).
*/
private boolean useSsl;
/**
* Used to keep track of number test e-mails.
*/
private static transient int testEmailCount = 0;
public DescriptorImpl() {
......@@ -273,6 +294,44 @@ public class Mailer extends Publisher {
}
}.process();
}
/**
* Send an email to the admin address
* @param req ignored request
* @param rsp used to write the result of the sending
* @throws IOException
* @throws ServletException
* @throws InterruptedException
*/
public void doSendTestMail(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException, InterruptedException {
rsp.setContentType("text/plain");
PrintStream writer = new PrintStream(rsp.getOutputStream());
try {
writer.println("Sending email to " + getAdminAddress());
writer.println();
writer.println("Email content ---------------------------------------------------------");
writer.flush();
MimeMessage msg = new MimeMessage(createSession());
msg.setSubject("Test email #" + ++testEmailCount);
msg.setContent("This is test email #" + testEmailCount + " sent from Hudson Continuous Integration server.", "text/plain");
msg.setFrom(new InternetAddress(getAdminAddress()));
msg.setSentDate(new Date());
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(getAdminAddress()));
msg.writeTo(writer);
writer.println();
writer.println("-----------------------------------------------------------------------");
writer.println();
writer.flush();
Transport.send(msg);
writer.println("Email was successfully sent");
} catch (MessagingException e) {
writer.println(e.getMessage());
}
writer.flush();
}
}
/**
......
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:section title="E-mail Notification">
<f:description>
<div align="right">
<a href="${rootURL}/publisher/Mailer/sendTestMail">Test configuration by sending e-mail to System Admin Address</a>
</div>
</f:description>
<f:entry title="SMTP server"
help="/help/tasks/mailer/smtp-server.html">
<input class="setting-input" name="mailer_smtp_server"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册