forked from greenmail-mail-test/greenmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MailCommand.java
61 lines (54 loc) · 1.96 KB
/
MailCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* Copyright (c) 2014 Wael Chatila / Icegreen Technologies. All Rights Reserved.
* This software is released under the Apache license 2.0
* This file has been used and modified.
* Original file can be found on http://foedus.sourceforge.net
*/
package com.icegreen.greenmail.smtp.commands;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.icegreen.greenmail.mail.MailAddress;
import com.icegreen.greenmail.smtp.SmtpConnection;
import com.icegreen.greenmail.smtp.SmtpManager;
import com.icegreen.greenmail.smtp.SmtpState;
/**
* MAIL command.
* <p/>
* <p/>
* The spec is at <a
* href="https://tools.ietf.org/html/rfc2821.html#section-4.1.1.2">
* https://tools.ietf.org/html/rfc2821.html#section-4.1.1.2</a>
* </p>
*/
public class MailCommand
extends SmtpCommand {
// "MAIL FROM:" ("<>" / Reverse-Path)
// [SP Mail-parameters] CRLF
static final Pattern PARAM = Pattern.compile("MAIL FROM:\\s?<([^>]*)>.*",
Pattern.CASE_INSENSITIVE);
@Override
public void execute(SmtpConnection conn, SmtpState state,
SmtpManager manager, String commandLine) {
Matcher m = PARAM.matcher(commandLine);
if (m.matches()) {
String from = m.group(1);
if (!from.isEmpty()) {
MailAddress fromAddr = new MailAddress(from);
String err = manager.checkSender(state, fromAddr);
if (err != null) {
conn.send(err);
return;
}
state.clearMessagePreservingAuthenticationState();
state.getMessage().setReturnPath(fromAddr);
conn.send("250 OK");
} else {
state.clearMessagePreservingAuthenticationState();
state.getMessage();
conn.send("250 OK");
}
} else {
conn.send("501 Required syntax: 'MAIL FROM:<email@host>'");
}
}
}