Example usage for javax.mail.internet MimeMessage setText

List of usage examples for javax.mail.internet MimeMessage setText

Introduction

In this page you can find the example usage for javax.mail.internet MimeMessage setText.

Prototype

@Override
public void setText(String text, String charset) throws MessagingException 

Source Link

Document

Convenience method that sets the given String as this part's content, with a MIME type of "text/plain" and the specified charset.

Usage

From source file:hudson.tasks.MailSender.java

private MimeMessage createFailureMail(AbstractBuild<?, ?> build, BuildListener listener)
        throws MessagingException, UnsupportedEncodingException, InterruptedException {
    MimeMessage msg = createEmptyMail(build, listener);

    msg.setSubject(getSubject(build, Messages.MailSender_FailureMail_Subject()), charset);

    StringBuilder buf = new StringBuilder();
    appendBuildUrl(build, buf);//from ww w  .  j av a 2  s  .  c o m

    boolean firstChange = true;
    for (ChangeLogSet.Entry entry : build.getChangeSet()) {
        if (firstChange) {
            firstChange = false;
            buf.append(Messages.MailSender_FailureMail_Changes()).append("\n\n");
        }
        buf.append('[');
        buf.append(entry.getAuthor().getFullName());
        buf.append("] ");
        String m = entry.getMsg();
        if (m != null) {
            buf.append(m);
            if (!m.endsWith("\n")) {
                buf.append('\n');
            }
        }
        buf.append('\n');
    }

    buf.append("------------------------------------------\n");

    try {
        // Restrict max log size to avoid sending enormous logs over email.
        // Interested users can always look at the log on the web server.
        List<String> lines = build.getLog(MAX_LOG_LINES);

        String workspaceUrl = null, artifactUrl = null;
        Pattern wsPattern = null;
        String baseUrl = Mailer.descriptor().getUrl();
        if (baseUrl != null) {
            // Hyperlink local file paths to the repository workspace or build artifacts.
            // Note that it is possible for a failure mail to refer to a file using a workspace
            // URL which has already been corrected in a subsequent build. To fix, archive.
            workspaceUrl = baseUrl + Util.encode(build.getProject().getUrl()) + "ws/";
            artifactUrl = baseUrl + Util.encode(build.getUrl()) + "artifact/";
            FilePath ws = build.getWorkspace();
            // Match either file or URL patterns, i.e. either
            // c:\hudson\workdir\jobs\foo\workspace\src\Foo.java
            // file:/c:/hudson/workdir/jobs/foo/workspace/src/Foo.java
            // will be mapped to one of:
            // http://host/hudson/job/foo/ws/src/Foo.java
            // http://host/hudson/job/foo/123/artifact/src/Foo.java
            // Careful with path separator between $1 and $2:
            // workspaceDir will not normally end with one;
            // workspaceDir.toURI() will end with '/' if and only if workspaceDir.exists() at time of call
            wsPattern = Pattern.compile("(" + Pattern.quote(ws.getRemote()) + "|"
                    + Pattern.quote(ws.toURI().toString()) + ")[/\\\\]?([^:#\\s]*)");
        }
        for (String line : lines) {
            line = line.replace('\0', ' '); // shall we replace other control code? This one is motivated by http://www.nabble.com/Problems-with-NULL-characters-in-generated-output-td25005177.html
            if (wsPattern != null) {
                // Perl: $line =~ s{$rx}{$path = $2; $path =~ s!\\\\!/!g; $workspaceUrl . $path}eg;
                Matcher m = wsPattern.matcher(line);
                int pos = 0;
                while (m.find(pos)) {
                    String path = m.group(2).replace(File.separatorChar, '/');
                    String linkUrl = artifactMatches(path, build) ? artifactUrl : workspaceUrl;
                    String prefix = line.substring(0, m.start()) + '<' + linkUrl + Util.encode(path) + '>';
                    pos = prefix.length();
                    line = prefix + line.substring(m.end());
                    // XXX better style to reuse Matcher and fix offsets, but more work
                    m = wsPattern.matcher(line);
                }
            }
            buf.append(line);
            buf.append('\n');
        }
    } catch (IOException e) {
        // somehow failed to read the contents of the log
        buf.append(Messages.MailSender_FailureMail_FailedToAccessBuildLog()).append("\n\n")
                .append(Functions.printThrowable(e));
    }

    msg.setText(buf.toString(), charset);

    return msg;
}