写一个shell脚本,每30分钟,从当前目录下随机选择一个.eml文件,作为邮件的主体发送到指定的邮箱
时间: 2024-06-01 21:11:28 浏览: 15
抱歉,我是AI语言模型,无法执行操作,以下是一个参考的脚本:
#!/bin/bash
while true
do
random_file=$(ls *.eml | shuf -n1) # 随机选择一个.eml文件
echo "Sending $random_file as email!"
# 发送邮件的命令,需要替换成实际的命令
# mail -s "Subject" -a "$random_file" recipient@example.com
sleep 1800 # 等待30分钟
done
相关问题
帮我用java写一个解析以.eml后缀结尾的邮件文件的方法
好的,可以使用JavaMail API来解析.eml文件。以下是一个基本的代码示例:
```
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
public class ParseEml {
public static void main(String[] args) {
String filePath = "your-file-path.eml";
parse(filePath);
}
public static void parse(String filePath) {
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
try (InputStream is = new FileInputStream(filePath)) {
Message message = new MimeMessage(session, is);
String subject = message.getSubject();
String from = getAddressListAsString(message.getFrom());
String to = getAddressListAsString(message.getRecipients(Message.RecipientType.TO));
String cc = getAddressListAsString(message.getRecipients(Message.RecipientType.CC));
String bcc = getAddressListAsString(message.getRecipients(Message.RecipientType.BCC));
String sentDate = message.getSentDate().toString();
String content = message.getContent().toString();
System.out.println("Subject: " + subject);
System.out.println("From: " + from);
System.out.println("To: " + to);
System.out.println("Cc: " + cc);
System.out.println("Bcc: " + bcc);
System.out.println("Sent Date: " + sentDate);
System.out.println("Content: " + content);
} catch (IOException | MessagingException e) {
e.printStackTrace();
}
}
private static String getAddressListAsString(Address[] addresses) {
StringBuilder sb = new StringBuilder();
if (addresses != null) {
for (Address address : addresses) {
sb.append(address.toString()).append(", ");
}
}
return sb.toString();
}
}
```
请注意,您需要替换代码示例中的 "your-file-path.eml " 字符串为您要解析的 .eml 文件的完整路径。
python 读取.eml文件
以下是使用Python读取.eml文件的示例代码:
```python
import email
# 读取.eml文件
with open('example.eml', 'rb') as f:
msg = email.message_from_binary_file(f)
# 获取邮件主题
subject = msg['subject']
# 获取发件人
from_addr = msg['from']
# 获取收件人
to_addr = msg['to']
# 获取邮件正文
Html = ''
Text = ''
for part in msg.walk():
# 获取邮件内容类型
content_type = part.get_content_type()
# 获取邮件编码方式
charset = part.get_charset()
# 如果是文本类型的附件
if content_type == 'text/plain':
Text += part.get_payload(decode=True).decode(charset)
# 如果是HTML类型的附件
elif content_type == 'text/html':
Html += part.get_payload(decode=True).decode(charset)
# 打印邮件信息
print('Subject:', subject)
print('From:', from_addr)
print('To:', to_addr)
print('Text:', Text)
print('Html:', Html)
```
以上代码使用Python标准库中的email模块读取.eml文件,并获取邮件的主题、发件人、收件人、文本内容和HTML内容。其中,使用了email.message_from_binary_file()函数读取.eml文件,使用了email.message.Message类获取邮件信息,使用了email.message.Message.walk()方法遍历邮件附件。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)